bison

How would I declare a grammar rule for a number of tags in any order?

家住魔仙堡 提交于 2019-12-13 04:25:10
问题 I am trying to write a compiler for a formating language.This language has a start and an end property and a set of document and text properties. The first is just info for the document itself where as the second is the actual document (titles, paragraphs, lists... the usual). The first set must always follow the start property and must contain all properties BUT in any order the user might like. Assuming that my tokens for the properites are PROP1, PROP2, PROP3 and PROP4 I can use recursion

Bison: How $ variables ($1 $2 etc) work with non-tokens?

百般思念 提交于 2019-12-13 02:42:27
问题 I'm wondering how $ variables work with non-tokens, like blocks of code. And my question can be reduced to this: I have a rule like this, with a block of code in the middle of it. In this case who is $3 and $4? func-header: ret-type ID { strcpy(func_id,current_id); } LPAREN params RPAREN 回答1: In the rule shown: ret-type is $1 . ID is $2 . The code block is $3 . LPAREN is $4 . params is $5 . RPAREN is $6 . In other words, code blocks act as non-terminals. 回答2: Mid-rule actions (MRA) are

Bison: how to fix reduce/reduce conflict

早过忘川 提交于 2019-12-13 02:40:51
问题 Below is a a Bison grammar which illustrates my problem. The actual grammar that I'm using is more complicated. %glr-parser %% s : e | p '=' s; p : fp | p ',' fp; fp : 'x'; e : te | e ';' te; te : fe | te ',' fe; fe : 'x'; Some examples of input would be: x x = x x,x = x,x x,x = x;x x,x,x = x,x;x,x x = x,x = x;x What I'm after is for the x's on the left side of an '=' to be parsed differently than those on the right. However, the set of legal "expressions" which may appear on the right of an

Simulating Booleans in Bison with C

孤街醉人 提交于 2019-12-13 02:16:16
问题 I am trying to make a logic calculator using C and bison, but I'm having trouble because C does not have a boolean type. This is part of my Flex rules: "TRUE" | "T" | "t" {yylval = 1; return TRUE; } "FALSE" | "F" | "f" {yylval = 0; return TRUE; } This is part of my Bison rules: line: EOL | exp EOL {printf("%d %d %d \n"), $1, $2,$$;} ; exp: TRUE | FALSE ; This is the output when I type T followed by EOL (end of line) in my calculator: 10 12 1 10 is ascii for newline, 12 is ascii for carriage

Bison semantic predicate syntax error, stray '#'

吃可爱长大的小学妹 提交于 2019-12-13 02:07:50
问题 So I am trying to use bison's semantic predicate feature, but I've been running into a few issues trying to have it work. The problem comes when I try to compile the generated .tab.c file with gcc. I am using gcc 7.1.0 and bison 3.0.4. Here's a snippet of the compile error: test2.tab.c: In function ‘yyuserAction’: test2.tab.c:811:12: error: stray ‘#’ in program if (! (#line 19 "test2.y" /* glr.c:816 */ ^ test2.tab.c:811:13: error: ‘line’ undeclared (first use in this function); did you mean

Initiating Short circuit rule in Bison for && and || operations

人盡茶涼 提交于 2019-12-12 12:15:50
问题 I'm programming a simple calculator in Bison & Flex , using C/C++ (The logic is done in Bison , and the C/C++ part is responsible for the data structures , e.g. STL and more) . I have the following problem : In my calculator the dollar sign $ means i++ and ++i (both prefix and postfix) , e.g. : int y = 3; -> $y = 4 -> y$ = 4 When the user hits : int_expression1 && int_expression2 , if int_expression1 is evaluated to 0 (i.e. false) , then I don't wan't bison to evaluate int_expression2 ! For

Can't figure out why Bison is throwing “Rules useless in parser due to conflicts”

一个人想着一个人 提交于 2019-12-12 12:11:42
问题 I'm writing a BNF grammar for a very simple programming language and using Flex and Bison to compile. I only have 3 variable and constant types: real, integer, string . My .l file has a token definition for "ID" as follows: DIGIT [0-9] LETTER [a-zA-Z] ID {LETTER}({LETTER}|{DIGIT})* My .y file has a definition for an identifier like this: identifier: ID; Now, I want to use the identifier definition to build variable and constant names. But I also want to limit assignment to data of the same

Why does this simple grammar have a shift/reduce conflict?

给你一囗甜甜゛ 提交于 2019-12-12 08:16:43
问题 %token <token> PLUS MINUS INT %left PLUS MINUS THIS WORKS: exp : exp PLUS exp; exp : exp MINUS exp; exp : INT; THIS HAS 2 SHIFT/REDUCE CONFLICTS: exp : exp binaryop exp; exp : INT; binaryop: PLUS | MINUS ; WHY? 回答1: This is because the second is in fact ambiguous. So is the first grammar, but you resolved the ambiguity by adding %left . This %left does not work in the second grammar, because associativity and precedence are not inherited from rule to rule. I.e. the binaryop nonterminal does

Compiling and executing the Shakespeare Programming Language translator spl2c on Mac OS X 10.6 results in warnings/errors

六眼飞鱼酱① 提交于 2019-12-12 07:56:40
问题 I wanted to experiment with the Shakespeare programming language, so I downloaded it from here and executed the Makefile using cd spl-1.2.1 Make . The compilation of spl2c executes with a couple warnings: scanner.l:600: warning, rule cannot be matched <stdout>:5808: warning: ‘yyunput’ defined but not used And then when it attempts to compile all the examples everything goes haywire: ../spl/bin/spl2c < fibonacci.spl > fibonacci.c Warning at line 19: equality expected Warning at line 28:

Is it possible to call one yacc parser from another to parse specific token substream?

若如初见. 提交于 2019-12-12 06:26:57
问题 Suppose I already have a complete YACC grammar. Let that be C grammar for example. Now I want to create a separate parser for domain-specific language, with simple grammar, except that it still needs to parse complete C type declarations. I wouldn't like to duplicate long rules from the original grammar with associated handling code, but instead would like to call out to the original parser to handle exactly one rule (let's call it "declarator"). If it was a recursive descent parser, there