Note that there are some explanatory texts on larger screens.

plurals
  1. POLex - recognizing tokens
    primarykey
    data
    text
    <p>I am trying to learn Lex. I have a simple program where i want to read in a file and recognize tokens.</p> <p>Right now i am getting some errors. I think i am having problems because there is more than one line in the file to recognize the tokens?</p> <p>Here is the file</p> <pre><code>fd 3x00 bk setc 100 int xy3 fd 10 rt 90 </code></pre> <p>here is the output i am trying to achieve:</p> <pre><code>Keyword: fd Illegal: 3x00 Keyword: bk Keyword: setc Number: 100 Keyword: int </code></pre> <p>here is my program:</p> <pre><code>%{ /* Comment */ #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; %} %% fd {printf("Keyword: fd\n");} [0-9][a-z][0-9] {printf("Illegal: 3x00\n");} bk {printf("Keyword: bk\n");} setc[0-9] {printf("Keyword: setc\n Number: %s\n", yytext);} int {printf("Keyword: int\n");} xy3 {printf("ID: xy3\n");} fd[0-9] {printf("Keyword: fd\n Number %s\n", yytext);} rt[0-9] {printf("Keyword: rt \n Number %s\n", yytext);} %% main( argc, argv) int argc; char** argv; { if(argc &gt; 1) { FILE *file; file = fopen(argv[1], "r"); if(!file) { fprintf(stderr, "Could not open %s \n", argv[1]); exit(1); } yyin = file; } yylex(); } </code></pre> <p>here are the errors i am getting when i try to compile it:</p> <pre><code> In function 'yylex': miniStarLogo.l:11: error: expected expression before '[' token miniStarLogo.l:11: error: 'a' undeclared (first use in this function) miniStarLogo.l:11: error: (Each undeclared identifier is reported only once miniStarLogo.l:11: error: for each function it appears in.) miniStarLogo.l:11: error: expected ';' before '{' token miniStarLogo.l:13: error: expected expression before '[' token miniStarLogo.l:13: error: expected ';' before '{' token </code></pre> <p>is the error in my printf statements? Thank you</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.
 

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