lalr

Error recovery in an LALR(1) grammar

一世执手 提交于 2019-12-10 11:23:41
问题 I'm using some parser and lexer generating tools (similar to Lex and Bison, but for C#) to generate programs that parse strings into abstract syntax trees that can later be evaluated. I wanted to do error recovery (i.e. report in the produced abstract sentence tree that there are missing tokens and such). I had two approaches in mind to structuring the generated grammars, and I was wondering which approach was better/more flexible/wouldn't have conflicts (the .y and .lex files are generated

How to solve a shift/reduce conflict?

两盒软妹~` 提交于 2019-12-09 16:15:48
问题 I'm using CUP to create a parser that I need for my thesis. I have a shift/reduce conflict in my grammar. I have this production rule: command ::= IDENTIFIER | IDENTIFIER LPAREN parlist RPAREN; and I have this warning: Warning : *** Shift/Reduce conflict found in state #3 between command ::= IDENTIFIER (*) and command ::= IDENTIFIER (*) LPAREN parlist RPAREN under symbol LPAREN Now, I actually wanted it to shift so I'm pretty ok with it, but my professor told me to find a way to solve the

How to fix YACC shift/reduce conflicts from post-increment operator?

霸气de小男生 提交于 2019-12-08 20:16:00
问题 I'm writing a grammar in YACC (actually Bison), and I'm having a shift/reduce problem. It results from including the postfix increment and decrement operators. Here is a trimmed down version of the grammar: %token NUMBER ID INC DEC %left '+' '-' %left '*' '/' %right PREINC %left POSTINC %% expr: NUMBER | ID | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | INC expr %prec PREINC | DEC expr %prec PREINC | expr INC %prec POSTINC | expr DEC %prec POSTINC | '(' expr ')' ; %% Bison

why is `int test {}` a function definition in C language BNF

十年热恋 提交于 2019-12-07 03:37:13
问题 I'm interested in the famous The syntax of C in Backus-Naur Form and studied for a while, what confuse me is that some syntax looks wrong to me but is considered right according to the BNF. For example, int test {} , what's this? I think this is a ill syntax in C, but the truth is the BNF considered this a function definition: int -> type_const -> type_spec -> decl_specs test-> id -> direct_declarator -> declarator '{' '}' -> compound_stat decl_specs declarator compound_stat -> function

SLR(1) and LALR(1) and Reduce

心不动则不痛 提交于 2019-12-06 09:32:07
问题 I confused Exactly !!!!!! I read following example in one of my professor note. 1) we have a SLR(1) Grammar G as following. we use SLR(1) parser generator and generate a parse table S for G. we use LALR(1) parser generator and generate a parse table L for G. S->AB A->dAa A-> lambda (lambda is a string with length=0) B->aAb Solution: the number of elements with R (reduce) in S is more than L. but in one site i read: 2) Suppose T1, T2 is created with SLR(1) and LALR(1) for Grammar G. if G be a

How to write LALR parser for some grammar in Java?

淺唱寂寞╮ 提交于 2019-12-06 09:11:13
问题 I want to write Java code to build a LALR parser for my grammar. Can someone please suggest some books or some links where I can learn how to write Java code for a LALR parser? 回答1: Writing a LALR parser by hand is difficult, but it can he done. If you want to learn the theory behind constructing parsers for them by hand, consider looking into "Parsing Techniques: A Practical Guide" by Grune and Jacobs. It's an excellent book on general parsing techniques, and the chapter on LR parsing is

Add error checking via production rules to LALR(1) grammar to handle all inputs

北慕城南 提交于 2019-12-06 08:05:58
I have a grammar that represents expressions. Let's say for simplicity it's: S -> E E -> T + E | T T -> P * T | P P -> a | (E) With a , + , * , ( and ) being the letters in my alphabet. The above rules can generate valid arithmetic expressions containing parenthesis, multiplication and addition using proper order of operations and associativity. My goal is to accept every string, containing 0 or more of the letters of my alphabet. Here are my constraints: The grammar must "accept" all strings contained 0 or more letters of my alphabet. New terminals may be introduced and inserted into the

why is `int test {}` a function definition in C language BNF

依然范特西╮ 提交于 2019-12-05 06:52:44
I'm interested in the famous The syntax of C in Backus-Naur Form and studied for a while, what confuse me is that some syntax looks wrong to me but is considered right according to the BNF. For example, int test {} , what's this? I think this is a ill syntax in C, but the truth is the BNF considered this a function definition: int -> type_const -> type_spec -> decl_specs test-> id -> direct_declarator -> declarator '{' '}' -> compound_stat decl_specs declarator compound_stat -> function_definition I tried this with bison, it considered the input int test {} is a right form, but I tried this on

SLR(1) and LALR(1) and Reduce

邮差的信 提交于 2019-12-04 17:00:17
I confused Exactly !!!!!! I read following example in one of my professor note. 1) we have a SLR(1) Grammar G as following. we use SLR(1) parser generator and generate a parse table S for G. we use LALR(1) parser generator and generate a parse table L for G. S->AB A->dAa A-> lambda (lambda is a string with length=0) B->aAb Solution: the number of elements with R (reduce) in S is more than L. but in one site i read: 2) Suppose T1, T2 is created with SLR(1) and LALR(1) for Grammar G. if G be a SLR(1) Grammar which of the following is TRUE? a) T1 and T2 has not any difference. b) total Number of

Generate an AST in C++

被刻印的时光 ゝ 提交于 2019-12-04 13:54:30
问题 I'm making an interpreter in C++, so far I've got my lexer to generate tokens. The problem is I'm not sure how to generate an "walk" a parse tree. I was thinking of making my parse tree using an array of arrays, but I'm not sure how to actually insert the tokens into the parse tree in the correct order. I'm not sure whether or not to go top-down, left-right or bottom-up, right-left. Can anyone provide me with a simple LALR(1) algorithm? 回答1: I'm going to go against conventional wisdom here