bison

How to get string value of token in flex and bison?

不羁的心 提交于 2019-12-04 10:58:29
I have this token in my .lex file: [a-zA-Z0-9]+ { yylval = yytext; return ALPHANUM; } and this code in my .y file: Sentence: "Sphere(" ALPHANUM ")." { FILE* file = fopen("C:/test.txt", "a+"); char st1[] = "polySphere -name "; strcat(st1, $2); strcat(st1, ";"); fprintf(file,"%s", st1); fclose(file); } I get this error when I try to compile: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast So $2 is an int, how do I make it a string? For example: "Sphere(worldGlobe)." I want $2 to have the string value worldGlobe here. Thanks for any help Aymanadou In the absence

Where to free memory in Bison/Flex?

时光怂恿深爱的人放手 提交于 2019-12-04 09:54:52
I'm using Bison & Flex for 1 month more or less, so I'm sorry if I don't see something obvious (but I don't think it is). I have a problem about freeing memory with Flex Bison. Here is what my code looks like: parser.l {DATE} { yylval.str= strdup(yytext); pair<string,string> newpair = make_pair("DATE",yytext); myvector.push_back(newpair); return TOKEN_DATE ;} This is one of the example of my .l file. I copy the value of yytext into yylval.str. Then I create a new pair with that content (key/value, actually), then I return the token date for bison. My parser .y is not more than yyparse; and

How to solve Bison warning “… has no declared type”

心已入冬 提交于 2019-12-04 09:50:32
问题 Running Bison on this file: %{ #include <iostream> int yylex(); void yyerror(const char*); %} %union { char name[100]; int val; } %token NUM ID %right '=' %left '+' '-' %left '*' %% exp : NUM {$$.val = $1.val;} | ID {$$.val = vars[$1.name];} | exp '+' exp {$$.val = $1.val + $3.val;} | ID '=' exp {$$.val = vars[$1.name] = $3.val;} ; %% Leads to warnings of the kind of: warning: $$ of 'exp' has no declared type. What does it mean and how do I solve it? 回答1: The union (%union) defined is not

BISON + FLEX grammar - why tokens are being concatenated together

丶灬走出姿态 提交于 2019-12-04 06:45:36
问题 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

How to pass the yytext from the lex file to yacc?

两盒软妹~` 提交于 2019-12-04 06:37:19
Please i am facing a simple problem.. here is the issue, In my lex file i have something similiar to: char *ptr_String; "name = " { BEGIN sName; } <sName>.+ { ptr_String = (char *)calloc(strlen(yytext)+1, sizeof(char)); strcpy(ptr_String, yytext); yylval.sValue = ptr_String; return NAME; } Now in my Yacc file i have something similar to: stmt_Name: NAME { /*Now here i need to get the matched string of <sName>.+ and measure it's length. */ /*The aim is simply outputing the name to the screen and storing the length in a global variable. } ; Please any suggestions? Thanks so much for all your

Problems with reentrant Flex and Bison

丶灬走出姿态 提交于 2019-12-04 06:09:19
I'm learning how to use reentrant Bison and Flex together. I already got a simple calculator working without the reentrant capability. However when I activated the reentrant feature and made the necessary modifications, I couldn't get this to work. Here is the code: scanner.l %{ #include <stdio.h> #include "parser.tab.h" %} %option 8bit reentrant bison-bridge %option warn noyywrap nodefault %option header-file="lex.yy.h" DIGIT [0-9] %% "+" { return ADD; } "-" { return SUB; } "*" { return MUL; } "/" { return DIV; } {DIGIT}+ { *yylval = atof(yytext); return NUM; } \n { return EOL; } [ \t] { } .

Make bison reduce to start symbol only if EOF is found

…衆ロ難τιáo~ 提交于 2019-12-04 05:57:31
问题 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

yylloc undefined in this scope

情到浓时终转凉″ 提交于 2019-12-04 05:30:53
问题 I have the following problem while compiling the files. I have overwritten the definition of YYLTYPE as follows(though it is the same as default but I will extend it typedef struct YYLTYPE { int first_line; int first_column; int last_line; int last_column; } YYLTYPE; and when I add the following in the lex file I get the "yylloc undefined in this scope" error. #define YY_USER_INIT yylloc.first_line = yylloc.first_column = 1; Pastebin: flex file bison file makefile 回答1: You need to put the

Flex++ Bisonc++ parser

五迷三道 提交于 2019-12-04 05:27:23
I'm trying to use flex and bison in my project to generate a parser code for a file structure. Main programming language is C++ and project is on an OO design mainly running in parallel. I heard that flex and bison generated parsers are C codes and they're not reenterant. Googling, I found flex++ and bisonc++ . Unfortunately there is no simple tutorial to get started. Most examples are based on bison/flex . Some people somehow integrated bison/flex parsers in their C++ code. They supposed to be "tricky"... Documentation of flex++ and bisonc++ doesn't help me and. Tutorials and examples, they

Flex/Bison: Error recovery destructors?

本小妞迷上赌 提交于 2019-12-04 04:03:45
问题 I have the following grammar for a comma-separated list with at least one item: column_expression_list: column_expression { $$ = LinkedList_New(); LinkedListItem *item = LinkedListItem_New($1); LinkedList_add($$, item); } | column_expression_list T_COMMA column_expression { LinkedListItem *item = LinkedListItem_New($3); LinkedList_add($1, item); } ; But consider this: column_expression error The $$ = LinkedList_New(); will leak. Is there a way I can set a destructor function for when this is