bison

Why are multi-line comments in flex/bison so evasive?

╄→гoц情女王★ 提交于 2019-12-06 18:08:13
问题 I'm trying to parse C-style multi-line comments in my flex (.l) file: %s ML_COMMENT %% ... <INITIAL>"/*" BEGIN(ML_COMMENT); <ML_COMMENT>"*/" BEGIN(INITIAL); <ML_COMMENT>[.\n]+ { } I'm not returning any token and my grammar (.y) doesn't address comments in any way. When I run my executable, I get a parse error: $ ./a.out /* abc def Parse error: parse error $ echo "/* foo */" | ./a.out Parse error: parse error (My yyerror function does a printf("Parse error: %s\n"), which is where the first

Tool to parse by grammar to AST (or .y+.lang => xml)

依然范特西╮ 提交于 2019-12-06 14:17:09
Given a lexer definition file, a grammar file (say, postgresql .y , .l flex and bison programs from it's source tree), and a file defined by those lexer and parser (say, an SQL query) to get the AST in some standard form (say, JSON of XML). The most important aspect of this tool is - flexibility of the input format. In my example, I could recreate postgres SQL grammar in ANTLR - but I don't want to. I'd rather just use whatever postgres is using. So even though .y file contains more than the parsing rules - the tool that I'm looking for will be able to understand them with minor modifications.

Making bison/flex parser reentrant with integral YYSTYPE

自作多情 提交于 2019-12-06 12:18:58
问题 I'm having trouble following the steps to make my bison/flex parser reentrant with a minimum amount of fuss. The problem appears to be in the lexer. Since everything parser is re-entrant, I can no longer assign yylval directly. Instead, according to the Flex manual, I have to call this function: void yyset_lval ( YYSTYPE * yylvalp , yyscan_t scanner ); But the problem is, YYSTYPE is an integral type. It isn't a dynamically allocated value, and it isn't an lvalue at all, so I can't pass a

Integrating Flex/Bison with external program

廉价感情. 提交于 2019-12-06 11:37:11
I'm working on an intelligent agent model that requires, as input, a list of events. The events come from the output of another model and are in a (large) text file. The text file is a list of all events (including unnecessary events that I don't care about), so I've written a scanner using flex that can find the useful bits. The framework for the intelligent agent model is already written in C++. Each event is timestamped and contains a large amount of information about the event. The format of the input file is constant, so I really have no need to check the syntax. I don't know if Bison

Is there a way to change the flex start state from bison?

混江龙づ霸主 提交于 2019-12-06 09:14:58
问题 I have defined different states in my lexer, which change not depending on the token but on a sequence of tokens (similarly to how template engines work). I can define longer tokens but I somehow like this approach better. 回答1: You can stick a function in the third section of the .l file that uses the BEGIN macro, and then call that function from your bison action (or anywhere else for that matter). You need to be careful of the fact that bison may read ahead a token before reducing a rule

bison/flex: Peek at the next letter or token

微笑、不失礼 提交于 2019-12-06 08:48:59
问题 When dealing with strings (it has its own state like comments) i need to find out if the next letter is a " or not. If it is i dont end the string state. So what happens is i just dont end the string in my string state (i use <STRING_STATE>. and process it letter by letter). So what happens is, i mark if the last string was " and if the current isnt i exit the state and unput the last letter. This has a weird effect. When i get errors on lines with strings i see the letter (usually a ',' or '

semantic type checking analysis in bison

岁酱吖の 提交于 2019-12-06 07:18:14
问题 I've been trying to find examples everywhere but it's been in vain. I am trying to write a basic Ruby interpreter. For this, I wrote a flex lexical file, containing token recognition sentences, and a grammar file. I wish for my grammar to contain semantic type checking. My grammar file contains, for example: arg : arg '+' arg This should be a valid rule for integers and floats. According to what I've read, I can specify type for a non terminal such as arg, like so: %type <intval> arg where

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

99封情书 提交于 2019-12-06 05:50:10
问题 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

Where to free memory in Bison/Flex?

跟風遠走 提交于 2019-12-06 04:47:17
问题 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

Why does my Mac (OS X 10.7.3) have an old version (2.3) of Gnu Bison?

邮差的信 提交于 2019-12-06 01:57:43
问题 The version of GNU Bison on my mac is 2.3 but I know that Bison 2.5 was released long ago. The book Flex & Bison uses version 2.5. Should I upgrade to 2.5 myself? Is it necessary? What's the difference between 2.3 and 2.5? 回答1: If you use Brew: brew tap homebrew/dupes && brew install bison If you use MacPorts: sudo port install bison If you use Fink, they're still on 2.3, just like Apple, so you'll have to edit the package yourself. If you know how to install Unix software, download it, untar