Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write flex and bison file parsing this language?
    primarykey
    data
    text
    <p>Let's define a language:</p> <pre><code>VAR := [0-9A-Za-z_]+ Exp := VAR | VAR,'=',VAR | '(', Exp, ')' | Exp, '&amp;', Exp | Exp ,'|', Exp </code></pre> <p>eg: "( a = b ) &amp; ( c | (d=e) ) "is legal</p> <p>I've read the YASS &amp; Lex manual, but I'm totally confused , I just want the compiler that can parse this language <br/> Can you tell me how to write the flex&amp;bison configure file for this language?</p> <p>I've done so far:</p> <p>file a.l:</p> <pre><code>%{ #include &lt;string.h&gt; #include "stdlib.h" #include "stdio.h" #include "y.tab.h" %} %% ("&amp;"|"and"|"AND") { return AND; } ("|"|"or"|"OR") { return OR; } ("="|"eq"|"EQ") { return EQ; } ([A-Za-z0-9_]+) { return VAR;} ("(") { return LB ;} (")") { return RB ;} ("\n") { return LN ;} %% int main(void) { yyparse(); return 0; } int yywrap(void) { return 0; } int yyerror(void) { printf("Error\n"); exit(1); } </code></pre> <p>file a.y</p> <pre><code>%{ #include &lt;stdio.h&gt; %} %token AND OR EQ VAR LB RB LN %left AND OR %left EQ %% line : | exp LN{ printf("LN: %s",$1);} ; exp: VAR { printf("var:%s",$1);} | VAR EQ VAR { printf("var=:%s %s %s",$1,$2,$3);} | exp AND exp { printf("and :%s %s %s",$1,$2,$3);} | exp OR exp { printf("or :%s %s %s",$1,$2,$3);} | LB exp RB { printf("abstract :%s %s %s",$1,$2,$3);} ; </code></pre> <p>Now I edited file as Chris Dodd guided,it seems much better(at least the lex worked fine),but I get output like this:</p> <pre><code>disk_path&gt;myprogram a=b var=:(null) (null) (null)LN: (null)ab=b Error </code></pre> <p>So, why the function printf output null? and after input the second ,it prompt Error and exit the program?</p>
    singulars
    1. This table or related slice is empty.
    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.
    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