yacc

bison shift instead of reduce. With reduce/reduce errors

拥有回忆 提交于 2019-12-21 22:32:40
问题 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

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

一个人想着一个人 提交于 2019-12-21 12:59:54
问题 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

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

半世苍凉 提交于 2019-12-21 12:59:30
问题 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

Parsing optional semicolon at statement end

回眸只為那壹抹淺笑 提交于 2019-12-21 08:02:09
问题 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

Is there a good Emacs mode or method for lex/flex/yacc/bison files?

帅比萌擦擦* 提交于 2019-12-21 06:48:44
问题 Editing lex or yacc files with Emacs is a nuisance: if I use C mode the indenting goes wrong, and if I don't use C mode I can't use indenting. Does anyone have a trick, a method, or an editing mode to get around it? 回答1: It's not entirely what you want but there is a very excelent Bison mode for Emacs. http://www.emacswiki.org/emacs/BisonMode This is a better link: http://ftp.lip6.fr/pub/emacs/elisp-archive/incoming/bison-mode.el.gz Since Yacc and bison are so closely related I don't find it

error handling in YACC

浪子不回头ぞ 提交于 2019-12-21 05:42:14
问题 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

How to use yylval with strings in yacc

天大地大妈咪最大 提交于 2019-12-21 05:06:10
问题 I want to pass the actual string of a token. If I have a token called ID, then I want my yacc file to actually know what ID is called. I thing I have to pass a string using yylval to the yacc file from the flex file. How do I do that? 回答1: See the Flex manual section on Interfacing with YACC. 15 Interfacing with Yacc One of the main uses of flex is as a companion to the yacc parser-generator. yacc parsers expect to call a routine named yylex() to find the next input token. The routine is

How to use yylval with strings in yacc

折月煮酒 提交于 2019-12-21 05:06:01
问题 I want to pass the actual string of a token. If I have a token called ID, then I want my yacc file to actually know what ID is called. I thing I have to pass a string using yylval to the yacc file from the flex file. How do I do that? 回答1: See the Flex manual section on Interfacing with YACC. 15 Interfacing with Yacc One of the main uses of flex is as a companion to the yacc parser-generator. yacc parsers expect to call a routine named yylex() to find the next input token. The routine is

Simple yacc grammars give an error

北慕城南 提交于 2019-12-21 04:54:11
问题 I have a question for yacc compiler. I do not compile simple yacc grammar. Here is the code section : /*anbn_0.y */ %token A B %% start: anbn '\n' {printf(" is in anbn_0\n"); return 0;} anbn: empty | A anbn B ; empty: ; %% #include "lex.yy.c" yyerror(s) char *s; { printf("%s, it is not in anbn_0\n", s); I use mac os x and, i try yo command; $ yacc anbn_0.y and then $ gcc -o anbn_0 y.tab.c -ll and give me error. Here is the error ; warning: implicit declaration of function 'yylex' is invalid

%left and %right in yacc

瘦欲@ 提交于 2019-12-21 04:23:26
问题 {% #include<stdio.h> #include<stdlib.h> %} %token ID NUM IF THEN LE GE EQ NE OR AND ELSE %right '=' %left AND OR %left '<' '>' LE GE EQ NE %left '+''-' %left '*''/' %right UMINUS %left '!' %% Mentioned Above is a part in the yacc program for a simple IF ELSE program.... i m just a beginner and don't understand what do we mean by %right and %left terms...... plz help me on this occasion... 回答1: %left and %right specify the associativity of an operator. The associativity of an operation