yacc

Resolving reduce/reduce conflict in yacc/ocamlyacc

ぐ巨炮叔叔 提交于 2019-12-05 01:24:43
I'm trying to parse a grammar in ocamlyacc (pretty much the same as regular yacc) which supports function application with no operators (like in Ocaml or Haskell), and the normal assortment of binary and unary operators. I'm getting a reduce/reduce conflict with the '-' operator, which can be used both for subtraction and negation. Here is a sample of the grammar I'm using: %token <int> INT %token <string> ID %token MINUS %start expr %type <expr> expr %nonassoc INT ID %left MINUS %left APPLY %% expr: INT { ExprInt $1 } | ID { ExprId $1 } | expr MINUS expr { ExprSub($1, $3) } | MINUS expr {

bison shift instead of reduce. With reduce/reduce errors

天涯浪子 提交于 2019-12-04 17:38:26
In my language i can write a = 1 b = 2 if true { } else { } if true { } **Here is the problem** else {} My grammer doesnt support newlines between statements. An else can only be used with an if. When i add optionalNL in my rule IfExpr: IF rval optionalNL codeBlock optionalNL ELSE codeBlock | IF rval optionalNL codeBlock The optionalNL before the else causes 3 reduce/reduce. Reason is it can reduce using the 2nd rule in IfExpr or reduce to exprLoop where it allows many newlines between expressions. No matter what i do (i tried writing %prec before optionalNL and ELSE) it always reduces to

error handling in YACC

微笑、不失礼 提交于 2019-12-04 15:51:13
hi there i'm trying to make a simple parser and using lex and yacc. the thing is i wanna print my own error messages rather than error symbol used by yacc which prints syntax error . for example this is my yacc code; %{ #include <stdio.h> #include <string.h> #include "y.tab.h" extern FILE *yyin; extern int linenum; %} %token INTRSW IDENTIFIER INTEGER ASSIGNOP SEMICOLON DOUBLEVAL DOUBLERSW COMMA %token IF ELSE WHILE FOR %token CLOSE_BRA OPEN_BRA CLOSE_PARA OPEN_PARA EQ LE GE %token SUM MINUS MULTIP DIV %left OPEN_BRA OPEN_PARA %left MULTIP DIV %left SUM MINUS %union { int number; char* string;

Yacc/Bison: The pseudo-variables ($$, $1, $2,..) and how to print them using printf

我的未来我决定 提交于 2019-12-04 12:34:43
I have a lexical analyser written in flex that passes tokens to my parser written in bison. The following is a small part of my lexer: ID [a-z][a-z0-9]* %% rule { printf("A rule: %s\n", yytext); return RULE; } {ID} { printf( "An identifier: %s\n", yytext ); return ID; } "(" return LEFT; ")" return RIGHT; There are other bits for parsing whitespace etc too. Then part of the parser looks like this: %{ #include <stdio.h> #include <stdlib.h> #define YYSTYPE char* %} %token ID RULE %token LEFT RIGHT %% rule_decl : RULE LEFT ID RIGHT { printf("Parsing a rule, its identifier is: %s\n", $2); } ; %% It

semantic type checking analysis in bison

↘锁芯ラ 提交于 2019-12-04 12:34:18
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 "intval" is in the type union and corresponds to the int C type. But this is only for integers, I am 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

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

Parsing optional semicolon at statement end

守給你的承諾、 提交于 2019-12-04 02:57:43
I was writing a parser to parse C-like grammars. First, it could now parse code like: a = 1; b = 2; Now I want to make the semicolon at the end of line optional. The original YACC rule was: stmt: expr ';' { ... } Where the new line is processed by the lexer that written by myself(the code are simplified): rule(/\r\n|\r|\n/) { increase_lineno(); return :PASS } the instruction :PASS here is equivalent to return nothing in LEX, which drop current matched text and skip to the next rule, just like what is usually done with whitespaces. Because of this, I can't just simply change my YACC rule into:

Lex/Flex - Scanning for the EOF character

僤鯓⒐⒋嵵緔 提交于 2019-12-04 02:56:16
问题 Other people have had the following problem that I am having but I can't find anyone that has reported a solution.. getting Flex to spot the EOF (end of file). I need Flex to find EOF and return a token indicating that it has found it so it can tell Yacc/Bison that it has reached the end of an input source file and can report a successful parse. Note that this question is not the same as this one because this is about Lex/Flex. Any help would be awesome. Thank you. 回答1: Flex has <<EOF>>