lex

Make bison reduce to start symbol only if EOF is found

时光怂恿深爱的人放手 提交于 2019-12-02 08:59:02
I am using Bison with Flex. I have the following rule in my Yacc input file: program : PROGRAM m2 declarations m0 block {cout << "Success\n"} ; The problem is that if I have a program that is partially correct, but then there is some "garbage" before EOF, it will reduce according to the previous rule, report "success" and only then report an error. I want to include EOF at the end of the rule above, but then, Flex would have to return EOF when it read <<EOF>> , and how would Bison know when to end the program? Now, I have this in Flex: <<EOF>> {return 0;} Here is an example that would do that:

flex / bison : how can I switch two lexers on same input file

ⅰ亾dé卋堺 提交于 2019-12-02 08:41:24
问题 How can I handover an open file e.g. read by another scanner to the next scanner - and give it to the parser ? 回答1: Flex buffers cannot easily be transferred from one scanner to another. Many details are private to the scanner and would need to be reverse-engineered, with the consequent loss of maintainability. However, it is not difficult to combine two (or more) scanner definitions into a single scanner, provided that the semantic types are compatible. It is simply necessary to give them

BISON + FLEX grammar - why tokens are being concatenated together

那年仲夏 提交于 2019-12-02 07:59:32
I would like to understand why BISON is concatenating two tokens on the following rule stmt: declaration { ... } | assignment { ... } | exp { ... } | ID ';' <-- this rule { ... fprintf(stderr, "\n my id is '%s'", $1); ... if you check the output will get what I mean. I run my parser and I input the characters ab; to the program. According to my bison grammar this should be parsed as an ID followed by a ; . And at some extent it is what happens. However, when I try to use the $1 variable of the rule ID ';' the program outputs ab; to me instead of ab . running the program ab; <-- this my input

What is regular expression for multi string?

时光总嘲笑我的痴心妄想 提交于 2019-12-02 07:13:24
I am learning to make a compiler and it's got some rules like single string: char ch[] ="abcd"; and multi string: printf("This is\ a multi\ string"); I wrote the regular expression STRING \"([^\"\n]|\\{NEWLINE})*\" It works fine with single line string but it doesn't work with multi line string where one line ends with a '\' character. What should I change? A common string pattern is \"([^"\\\n]|\\(.|\n))*\" This will match strings which include escaped double quotes ( \" ) and backslashes ( \\ ). It uses \\(.|\n) to allow any character after a backslash. Although some backslash sequences are

flex / bison : how can I switch two lexers on same input file

独自空忆成欢 提交于 2019-12-02 04:24:36
How can I handover an open file e.g. read by another scanner to the next scanner - and give it to the parser ? Flex buffers cannot easily be transferred from one scanner to another. Many details are private to the scanner and would need to be reverse-engineered, with the consequent loss of maintainability. However, it is not difficult to combine two (or more) scanner definitions into a single scanner, provided that the semantic types are compatible. It is simply necessary to give them different start conditions. Since the start condition can be set even outside of a scanner action, it is

Ply Lex parsing problem

ぐ巨炮叔叔 提交于 2019-12-01 17:23:00
I'm using ply as my lex parser. My specifications are the following : t_WHILE = r'while' t_THEN = r'then' t_ID = r'[a-zA-Z_][a-zA-Z0-9_]*' t_NUMBER = r'\d+' t_LESSEQUAL = r'<=' t_ASSIGN = r'=' t_ignore = r' \t' When i try to parse the following string : "while n <= 0 then h = 1" It gives following output : LexToken(ID,'while',1,0) LexToken(ID,'n',1,6) LexToken(LESSEQUAL,'<=',1,8) LexToken(NUMBER,'0',1,11) LexToken(ID,'hen',1,14) ------> PROBLEM! LexToken(ID,'h',1,18) LexToken(ASSIGN,'=',1,20) LexToken(NUMBER,'1',1,22) It doesn't recognize the token THEN, instead it takes "hen" as an identifier

Flex and Yacc - Cannot find - lfl?

旧巷老猫 提交于 2019-12-01 16:21:02
Hi I'm learing Lex and yacc. I created the following lex program. %{ #include <stdio.h> %} %% [0123456789]+ printf("NUMBER\n"); [a-zA-Z][a-zA-Z0-9]* printf("WORD\n"); %% I'm trying to run it using the following commands: lex example1.l cc lex.yy.c -o example1 -ll also tried cc lex.yy.c -o example1 -lfl When I enter the second command form above, I get error: D:\workdir\flexyacc\Test3>gcc lex.yy.c -o Test -lfl C:\Dev-Cpp\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\mingw32\bin\ld.exe: cannot find -lfl collect2: ld returned 1 exit status I tried googling this error but no luck so far. Since I'm new

How do I remove the following 'implicit declaration of function' warnings?

六月ゝ 毕业季﹏ 提交于 2019-12-01 15:17:36
How do I compile the lex file with gcc without receiving the following warnings? lex.yy.c: In function `yy_init_buffer': lex.yy.c:1688: warning: implicit declaration of function `fileno' lex.l: In function `storeLexeme': lex.l:134: warning: implicit declaration of function `strdup' These are the libraries I included. %{ #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> %} The function yy_init_buffer is not in the file. The following is the function storeLexeme. int storeLexeme() { for (int i = 0; i < count; i++) { char *curr = *(symbolTable + i); if (strcmp(curr,

Flex and Yacc - Cannot find - lfl?

泄露秘密 提交于 2019-12-01 15:14:10
问题 Hi I'm learing Lex and yacc. I created the following lex program. %{ #include <stdio.h> %} %% [0123456789]+ printf("NUMBER\n"); [a-zA-Z][a-zA-Z0-9]* printf("WORD\n"); %% I'm trying to run it using the following commands: lex example1.l cc lex.yy.c -o example1 -ll also tried cc lex.yy.c -o example1 -lfl When I enter the second command form above, I get error: D:\workdir\flexyacc\Test3>gcc lex.yy.c -o Test -lfl C:\Dev-Cpp\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\mingw32\bin\ld.exe: cannot find

lex & yacc get current position

断了今生、忘了曾经 提交于 2019-12-01 12:59:17
In lex & yacc there is a macro called YY_INPUT which can be redefined, for example in a such way #define YY_INPUT(buf,result,maxlen) do { \ const int n = gzread(gz_yyin, buf, maxlen); \ if (n < 0) { \ int errNumber = 0; \ reportError( gzerror(gz_yyin, &errNumber)); } \ \ result = n > 0 ? n : YY_NULL; \ } while (0) I have some grammar rule which called YYACCEPT macro. If after YYACCEPT I called gztell (or ftell), then I got a wrong number, because parser already read some unnecessary data. So how I can get current position if I have some rule which called YYACCEPT in it(one bad solution will be