flex-lexer

Only part of matched string in flex in yytext

旧时模样 提交于 2020-07-09 03:07:26
问题 I'm a newbie but I would like to know if I can, with flex, parse something with regex in a way yytext would only be part of the matched sequence. For example: @abcd{efgh, . I would like to match abcd once and use it then efgh, but I need to use the @ and the { to match them. Is this possible or do I have to process it entirely later in C? 回答1: You can use following context with the '/' operator. For eaxmple, abcd/efgh will match the string "abcd" only if its followed by "efgh", with the

Copying entire input line in (f)lex (for better error messages)?

落爺英雄遲暮 提交于 2020-06-26 04:35:20
问题 As part of a typical parser using yacc (or bison) and lex (or flex), I'd like to copy entire input lines in the lexer so that, if there's an error later, the program can print out the offending line in its entirety and put a caret ^ under the offending token. To copy the line, I'm currently doing: char *line; // holds copy of entire line bool copied_line; %% ^.+ { if ( !copied_line ) { free( line ); line = strdup( yytext ); copied_line = true; } REJECT; } /* ... other tokens ... */ \n {

How can I debug my flex/bison grammar?

ⅰ亾dé卋堺 提交于 2020-05-29 07:29:48
问题 This is a very silly problem. There are no errors in the grammar rules afaik but its not giving the right output. I have been staring at it but the mistake is not visible to me. What tools are available to me to help me see what is going on in a parse? My attempts to insert tracing code are a lot of work and don't seem to be helping me much. parser.y %{ #include<stdio.h> #include<stdlib.h> #include<string.h> #include "SymbolTable.h" #include "SymbolInfo.h" #include "ScopeTable.h" int yyparse

Bison : Line number included in the error messages

北慕城南 提交于 2020-05-22 07:11:44
问题 OK, so I suppose my question is quite self-explanatory. I'm currently building a parser in Bison, and I want to make error reporting somewhat better. Currently, I've set %define parse.error verbose (which actually gives messages like syntax error, unexpected ***********************, expecting ******************** . All I want is to add some more information in the error messages, e.g. line number (in input/file/etc) My current yyerror (well nothing... unusual... lol) : void yyerror(const char

Flex and Bison: Beginning a sentence with a specific keyword

给你一囗甜甜゛ 提交于 2020-01-17 04:59:32
问题 I am working on a program using Flex and Bison. My task can be done using only Flex(using start conditions etc.), but I have understood that using Bison might make my life easier. My task is to design a program which recognizes a programming language declaration part. Its syntax and logic can be understood through my code below. My problem is that I want my program to recognize as an acceptable declaration part every part of code which begins only with the " var " keyword! Until now, I have

Syntax analyser to show success using flex and bison

我只是一个虾纸丫 提交于 2020-01-16 02:52:51
问题 I am trying to make a syntax analyzer that will recognize a valid statement and will print success upon doing so. However, after making the lex and yacc files, I keep getting errors in my yacc file which says: In function 'yyparse'fofo.y: In function 'yyparse': fofo.y:13:5: error: stray '\223' in program fofo.y:13:5: error: stray '\' in program fofo.y:13:16: error: 'n' undeclared (first use in this function) fofo.y:13:16: note: each undeclared identifier is reported only once for each

flex yy_fatal_error exist just like that. I want handler back to application

筅森魡賤 提交于 2020-01-15 12:12:47
问题 flex yy_fatal_error exist just like that. But I want handler back to my application. How to avoid exist call? from yy_fatal_error. whether this problem addressed in any version? your suggestion is highly appreciated. help me on this issues. 回答1: You can override the function, by #define ing your own. Note that in the generated code there is /* Report a fatal error. */ #ifndef YY_FATAL_ERROR #define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) #endif If you #define the macro YY_FATAL_ERROR(msg)

Detecting and skipping line comments with Flex

牧云@^-^@ 提交于 2020-01-10 04:25:47
问题 How can I detect one line comments like "//" in Flex and skip this line? And Also: In case of the "/*" comments im using the bellow. Will it work? "/*" { comment(); } %% comment() { char c, c1; loop: while ((c = input()) != '*' && c != 0) putchar(c); if ((c1 = input()) != '/' && c != 0) { unput(c1); goto loop; } if (c != 0) putchar(c1); } 回答1: Why don't you just use regular expressions to recognize the comments? The whole point of lex/flex is to save you from having to write lexical scanners

Character-by-character description of flex scanner

泪湿孤枕 提交于 2020-01-07 09:20:52
问题 I am having a really hard time tracking down a bug in a rather large flex/bison parser (1000 grammar rules, 1500 states, 400 terminals). The scanner matches a terminal that should not arise at this particular point and is not present in the data file. The input I am trying to parse is <el Re="1.0" Im="-1.0"/> and the last few lines of the output are Reading a token: Next token is token ELEMENTTEXT (1.1-1.1: ) matched 4 characters: Re= matched 1 characters: " matched 6 characters: -1 Im= This

how to execute code after yylex(); command

最后都变了- 提交于 2020-01-06 14:06:01
问题 I have a simple flex source code which skips the comments in /* */ and should get the count of comments found: %{ int in_comment = 0; int count = 0; %} %% \/\* { in_comment = 1; count++; } \*\/ { in_comment = 0; } . { if (!in_comment) ECHO; } %% int main(void) { yylex(); printf("Comments found %d\n", count); // never executed return 0; } First half works fine - it really skips the comments, but they are not counted... what can I do to execute printf line? 回答1: I just tried it myself. So I