質問

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

%{ 
#include <stdio.h>
#include "mylang3.tab.h"
#include <stdlib.h>
#include <string.h>
#define OK 234
#define ILLEGAL 235
%}

digit       [0-9]
letter      [a-zA-Z]
invalid_id  [char|else|if|class|new|return|void|while]
unsigned_int    ({digit}+)
INTEGER     ((+|-)?{unsigned_int})
all_chars   [{letter}{digit}_]
ID              ([({all_chars}{-}{digit})({all_chars})*]{-}{invalid_id})
special_char    ["\n"|"\""|"\'"|"\0"|"\t"|"\\"]
CHARACTER   '([[:print:]]{-}["']{+}{special_char})'


%%
[a-zA-Z]+           printf("I have found a word %s\n", yytext);
{ID}                printf("I have found an id %s\n", yytext);     //errors
{INTEGER}           printf("I have found an integer %s\n",yytext); //errors
{CHARACTER}         printf("I have found a char %s\n",yytext);     //errors
char|else|if|class|new|return|void|while    printf("I have found a reserved word %s\n",yytext);
"+"|"-"|"*"|"/"|"{"|"}"|"("|")"|"["|"]"     printf("I have found an operator: %s\n", yytext );
[" " \t\n]+                 /* eat up whitespace */
.                       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==ILLEGAL){ printf("Illegal sequence\n"); ok=0; }
    }
    if(ok==0) printf("Encountered lexical errors\n");
    else printf("No lexical errors found\n");
    return 0;
}*/
役に立ちましたか?

解決

You can only use square brackets for characters, not for sequences of characters. So instead of e. g.

all_chars   [{letter}{digit}_]

you'll have to write

all_chars   ({letter}|{digit}|_)

And you shouldn't mix pipe signs and square brackets. [abc] means the same as (a|b|c), but [a|b|c] is wrong.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top