Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your lex rule <code>("[0-9A-Za-z_]+")</code> will match (only) the literal string <code>[0-9A-Za-z_]+</code> -- get rid of the <code>"</code> characters to have it be a pattern to match any identifier or number.</p> <p>Your yacc code does not match your lex code for punctuation -- the lex code returns <code>AND</code> for <code>&amp;</code> while the yacc code is expecting an <code>&amp;</code> -- so either change the lex code to return <code>'&amp;'</code> or change the yacc code to use the token <code>AND</code>, and similarly for <code>|</code>, <code>(</code>, and <code>)</code>. You might also want to ignore spaces in the lex code (rather than treating them as errors). You also have no lex rule to match and return <code>'\n'</code>, even though you use that in your yacc grammar.</p> <p>Your yacc code is otherwise correct, but is ambiguous, thus giving you shift/reduce conflicts. That's because your grammar is ambiguous -- an input like <code>a&amp;b|c</code> can be parsed as either <code>(a&amp;b)|c</code> or <code>a&amp;(b|c)</code>. You need to decide how that ambiguity should be resolved and reflect that in your grammar -- either by using more non-terminals, or by using yacc's built-in precedence support for resolving this kind of ambiguity. If you stick the declarations:</p> <pre><code>%left '|' %left '&amp;' </code></pre> <p>in the top of your yacc file, that will resolve the ambiguity by making both <code>&amp;</code> and <code>|</code> left associative, and <code>&amp;</code> higher precedence than <code>|</code>, which would be the normal interpretation.</p> <p><strong>Edit</strong></p> <p>The problem you have now is that you never define YYSTYPE (either directly or with %union) in your .y file and you never set yylval in your .l file. The first problem means that <code>$1</code> etc are just <code>int</code>s, not pointers (so it makes no sense to try to print them with <code>%s</code> -- you should be getting a warning from your C compiler over that). The second problem means that they never have a value anyways, so its just always the default 0 value of an uninitialized global variable</p> <p>The easiest fix would be to add</p> <pre><code>%union { const char *name; } %token &lt;name&gt; VAR LB RB LN %left &lt;name&gt; AND OR %left &lt;name&gt; EQ %type &lt;name&gt; expr </code></pre> <p>to the top of the yacc file. Then change the all the lex rules to be something like</p> <pre><code>([A-Za-z0-9_]+) { yylval.name = strdup(yytext); return VAR;} </code></pre> <p>Finally, you also need to change the bison actions for expr to set <code>$$</code>, eg:</p> <pre><code>| LB exp RB { asprintf(&amp;$$, "%s %s %s",$1,$2,$3); printf("abstract: %s\n", $$); } </code></pre> <p>This will at least work, though it will leak lots of memory for the allocated strings.</p> <p>The last problem you have is that your <code>line</code> rule only matches a single line, so a second line of input causes an error. You need a recursive rule like:</p> <pre><code>line: /* empty */ | line exp LN { printf.... </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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