Tuesday 15 January 2013

lexical analysis - Error when using Flex to generate a scanner (unrecognised rule) -


I am trying to generate a simple scanner using Flex. However, when using the following code, I get many "unrecognized rule" errors on lines 23,24 and 25. After studying some similar examples, I still can not find any formatting mistakes. Can anyone point me in the right direction?

 % {#include & lt; Stdio.h & gt; #include "mylang3.tab.h" # Include & lt; Stdlib.h & gt; #include & lt; String.h & gt; #define OK 234 # Unknown 235%} Defined digits [0-9] Characters [a-zA-Z] invalid_id [char | Else | If | Class | New | Return | Zero] while] unsigned ({digits} +) INTEGER All_chars {{letter} {digits} _] id ([({all_chars} {-} {digits}} ({all_chars}) *] {-} {invalid_id}) Special_char ["\ \" \ "" \ "" \ "" \ "" \ "" \ "" | " ''% [[A-zA-Z] + printf ("I have a word% s \ n" ("[\: print:]] {[: print:]] {-} ['] {+} {special_ature} , Yytext found); {ID} printf ("I got an id% s \ n", yytext); // errors {INTEGER} printf ("I got an integer% s \ n", yytext); // Errors {CHARACTER} printf ("I got a four% s \ n", yytext); // Errors Four | And | If Class | New | Return | Zero | Whereas printf ("I got a reserved word% s \ n", yytext); "" "" | "-" | "{" | "}" | "(" | ")" | "[" | "]" Printf ("I found an operator:% s \ N", yytext); ["" \ T \ n] + / * Eat white spot * / Printf ("Unrecognized character:% s \ n", yytext); %% / * int main (int argc, char ** argv) {int token; Int ok = 1; ++ argv, --argc; If (argc> 0) yyin = fopen (argv [0], "r"); Else yyin = stdin; Yylex (); While ((token = yylex ())! = 0) {if (token == invalid) {printf ("Invalid order \ n"); Ok = 0; }} If (fine == 0) printf ("expected lexical errors \ n"); Other printf ("no lexical error was found \ n"); Return 0; } * /    

You are not just for the sequence of characters, square brackets for letters only You can use . Then instead of E, G. You must write

  all_chars {Digit} | _)   

and you should not mix pipe signs and square brackets. [ABC] is similar to the meaning (A | B | C), but [A | B C] is wrong.

No comments:

Post a Comment