Note that there are some explanatory texts on larger screens.

plurals
  1. POFlex/bison syntax error
    primarykey
    data
    text
    <p>I am trying to write a grammar which will be able to consume the following input:</p> <pre><code>begin #this is a example x = 56; while x &gt; 0 do begin point 15.6 78.96; end; end; </code></pre> <p>Here is the lexer.l file:</p> <pre><code>%option noyywrap %{ #include "parser.h" #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; const char * const unrecognizedToken = "Unrecognized token"; %} NewLine \n WhiteSpaces [\r\t\f\v ]+ Semicolon ; digit [0-9] number1 {digit}+\.?([eE][-+]?{digit}+)? number2 {digit}*\.{digit}+([eE][-+]?{digit}+)? double_number {number1}|{number2} BEGIN "begin" END "end" WHILE "while" DO "do" POINT "point" %x POINT_DEFINITIONS %% {WhiteSpaces} { printf("WhiteSpaces"); printf("\n"); } {NewLine} { printf("NewLine"); printf("\n"); } {WHILE} { printf("While"); printf("\n"); return TOKEN_WHILE; } {BEGIN} { printf("TOKEN_BEGIN"); printf("\n"); return TOKEN_BEGIN; } {END} { printf("TOKEN_END"); printf("\n"); return TOKEN_END; } {DO} { printf("DO"); printf("\n"); return TOKEN_DO; } {POINT} { printf("POINT_START"); printf("\n"); BEGIN POINT_DEFINITIONS; return TOKEN_POINT; } &lt;POINT_DEFINITIONS&gt;{double_number} { printf("POINT_DEFINITIONS %s", yytext); printf("\n"); yylval.dval = atof(yytext); return TOKEN_DOUBLE; } &lt;POINT_DEFINITIONS&gt;{WhiteSpaces} { printf("WhiteSpaces"); printf("\n"); } [a-zA-Z_][a-zA-Z0-9_]* { printf("TOKEN_IDENTIFIER"); printf("\n"); yylval.name = strdup(yytext); return TOKEN_IDENTIFIER; } [()=;] { printf("yytext = %s", yytext); printf("\n"); return *yytext; } [*/+\-&lt;&gt;] { printf("TOKEN_OPERATOR"); printf("\n"); yylval.op = *yytext; return TOKEN_OPERATOR; } [-]?[0-9]+ { printf("TOKEN_VALUE"); printf("\n"); yylval.val = atoi(yytext); return TOKEN_VALUE; } #.* { printf("COMMENT"); printf("\n"); /*comment*/ } . { printf("%s", unrecognizedToken); } </code></pre> <p>And here is the parser.y file:</p> <pre><code>%error-verbose %{ #define YYDEBUG 1 %} %union { int val; double dval; char op; char* name; } %token TOKEN_BEGIN TOKEN_END TOKEN_WHILE TOKEN_DO TOKEN_POINT TOKEN_OPERATOR TOKEN_VALUE TOKEN_IDENTIFIER TOKEN_DOUBLE %start program %{ void yyerror(const char* const message); %} %% program: statement';'; block: TOKEN_BEGIN statements TOKEN_END { printf("rule block\n"); }; statements: statement';' statements { printf("rule statements\n"); } |; statement: | assignment | command | whileStmt | block; assignment: TOKEN_IDENTIFIER '=' TOKEN_VALUE { printf("rule Assignment\n"); } ; whileStmt: TOKEN_WHILE condition TOKEN_DO block {printf("rule While\n");}; condition: TOKEN_IDENTIFIER { printf("rule token_identifier\n"); } | TOKEN_VALUE { printf("rule token_value\n"); } | condition TOKEN_OPERATOR condition { printf("rule condition TOKEN_OPERATOR condition\n"); }; command: TOKEN_POINT TOKEN_DOUBLE TOKEN_DOUBLE { printf("rule Command\n"); }; %% #include &lt;stdlib.h&gt; void yyerror(const char* const message) { printf("Parse error:%s\n", message); exit(1); } int main() { yyparse(); } </code></pre> <p>But am getting the following error message:</p> <pre><code>Parse error:syntax error, unexpected $end, expecting ';' </code></pre> <p>Compiled like this:</p> <pre><code> flex -o lexer.c lexer.l bison -v -d -o parser.c parser.y gcc parser.c lexer.c -o parser -g -DYYDEBUG=1 </code></pre> <p>To run the parser:</p> <pre><code>./parser &lt; example </code></pre> <p>Could you please help me to find out what is the problem? Why grammar cannot accept the above example as an input?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload