Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make lex/flex recognize tokens not separated by whitespace?
    primarykey
    data
    text
    <p>I'm taking a course in compiler construction, and my current assignment is to write the lexer for the language we're implementing. I can't figure out how to satisfy the requirement that the lexer must recognize concatenated tokens. That is, tokens not separated by whitespace. E.g.: the string <code>39if</code> is supposed to be recognized as the number <code>39</code> and the keyword <code>if</code>. Simultaneously, the lexer must also <code>exit(1)</code> when it encounters invalid input.</p> <p>A simplified version of the code I have:</p> <pre><code>%{ #include &lt;stdio.h&gt; %} %option main warn debug %% if | then | else printf("keyword: %s\n", yytext); [[:digit:]]+ printf("number: %s\n", yytext); [[:alpha:]][[:alnum:]]* printf("identifier: %s\n", yytext); [[:space:]]+ // skip whitespace [[:^space:]]+ { printf("ERROR: %s\n", yytext); exit(1); } %% </code></pre> <p>When I run this (or my complete version), and pass it the input <code>39if</code>, the error rule is matched and the output is <code>ERROR: 39if</code>, when I'd like it to be:</p> <pre><code>number: 39 keyword: if </code></pre> <p>(I.e. the same as if I entered <code>39 if</code> as the input.)</p> <p><a href="http://westes.github.io/flex/manual/Matching.html" rel="nofollow noreferrer">Going by the manual</a>, I have a hunch that the cause is that the error rule matches a longer possible input than the number and keyword rules, and flex will prefer it. That said, I have no idea how to resolve this situation. It seems unfeasible to write an explicit regexp that will reject all non-error input, and I don't know how else to write a "catch-all" rule for the sake of handling lexer errors.</p> <p><strong>UPDATE:</strong> I suppose I could just make the catch-all rule be <code>. { exit(1); }</code> but I'd like to get some nicer debug output than "I got confused on line 1".</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