context-free-grammar

Purpose of FIRST and FOLLOW sets in LL(1) parsers?

北城以北 提交于 2020-07-04 05:34:44
问题 Can anyone explain to me how FIRST and FOLLOW should be used in LL(1) grammar? I understand that they are used for syntax table construction, but I don't understand how. 回答1: In an LL(1) parser, the parser works by maintaining a workspace initially seeded to the start symbol followed by the end-of-string marker (usually denoted $). At each step, it does one of the following: If the first symbol of the workspace is a terminal, it matches it against the next token of input (or reports an error

Purpose of FIRST and FOLLOW sets in LL(1) parsers?

假如想象 提交于 2020-07-04 05:33:31
问题 Can anyone explain to me how FIRST and FOLLOW should be used in LL(1) grammar? I understand that they are used for syntax table construction, but I don't understand how. 回答1: In an LL(1) parser, the parser works by maintaining a workspace initially seeded to the start symbol followed by the end-of-string marker (usually denoted $). At each step, it does one of the following: If the first symbol of the workspace is a terminal, it matches it against the next token of input (or reports an error

How to do Priority of Operations (+ * - /) in my grammars?

爷,独闯天下 提交于 2020-05-23 19:17:17
问题 I define my own grammars using antlr 4 and I want to build tree true According to Priority of Operations (+ * - /) .... I find sample on do Priority of Operations (* +) it work fine ... I try to edit it to add the Priority of Operations (- /) but I failed :( the grammars for Priority of Operations (+ *) is : println:PRINTLN expression SEMICOLON {System.out.println($expression.value);}; expression returns [Object value]: t1=factor {$value=(int)$t1.value;} (PLUS t2=factor{$value=(int)$value+

How to modify parsing grammar to allow assignment and non-assignment statements?

丶灬走出姿态 提交于 2020-01-24 15:46:11
问题 So the question is about the grammar below. I'm working on a mini-interpreted language for fun (we learned about some compiler design in class, so I want to take it to the next level and try something on my own). I'm stuck trying to make the non-terminal symbol Expr . Statement ::= Expr SC Expr ::= /* I need help here */ Assign ::= Name EQUAL Expr AddSub ::= MulDiv {(+|-) AddSub} MulDiv ::= Primary {(*|/) MulDiv} Primary ::= INT | FLOAT | STR | LP Expr RP | Name Name ::= ID {. Name} Expr has

Shift-reduce: when to stop reducing?

微笑、不失礼 提交于 2020-01-23 07:43:51
问题 I'm trying to learn about shift-reduce parsing. Suppose we have the following grammar, using recursive rules that enforce order of operations, inspired by the ANSI C Yacc grammar: S: A; P : NUMBER | '(' S ')' ; M : P | M '*' P | M '/' P ; A : M | A '+' M | A '-' M ; And we want to parse 1+2 using shift-reduce parsing. First, the 1 is shifted as a NUMBER. My question is, is it then reduced to P, then M, then A, then finally S? How does it know where to stop? Suppose it does reduce all the way

How Compiler distinguishes minus and negative number during parser process

亡梦爱人 提交于 2020-01-15 03:58:06
问题 Hey I'm recently involved in a compiler developer, I've encountered a problem with minus sign(-) and negative number(-1). Suppose now I have 5--3, 5+-3, how to write a grammar rule such that during the abstract syntax tree construction, yacc will produce a correct abstract syntax tree? My grammar is like: expr : constant {} | id {} | exec_expr {} exec_expr : expr PLUS expr {} | expr MINUS expr {} | expr MUL expr {} | expr DIV expr {} My thought for now is to have a UMINUS symbol with highest

Can a BNF handle forward consumption?

梦想的初衷 提交于 2020-01-14 11:52:53
问题 Recently I've discovered the python module pyparsing , a wonderful tool for parsing data by writing the grammar , not the parser. I'm new to the idea of context-free grammars, so please correct any false assumptions in this question. Pyparsing can implement a BNF (Backus–Naur Form) context-free grammar. This grammar can be recursive, but can it have a forward look-ahead? I've been wondering about the answer to this since I stumbled across this question. Let me give you a concrete example.

Find a grammar of binary number divisible by 5 with 1 as MSB

青春壹個敷衍的年華 提交于 2020-01-14 03:41:08
问题 How can I find a grammar of binary number divisible by 5 with 1 as MSB and find the reversal of L So, I need a grammar that generates numbers like.. 5 = 101 10 = 1010 15 = 1111 20 = 10100 25 = 110011 and so on 回答1: I'm assuming this is homework and you just want a hint. Let's consider a somewhat similar question, but in base 10. How can we write a CFG for numbers divisible by 3. At first glance, this seems unlikely, but it's actually pretty simple. We start with the observation that: 10 k ≅ 1

How to tell the precedence of operators in a context free grammar

♀尐吖头ヾ 提交于 2020-01-13 19:34:09
问题 How can we know which of the following logical operations ( or, and, not ) in the bellow context free grammar have higher precedence? Is there a general approach to this kind of problems? X → X or Y | Y Y → Y and Z | Z Z → not Z | (X) | true | false 回答1: Here's an example approach: expr -> addExpr; addExpr -> multExpr (('+'|'-') multExpr)*; multExpr -> terminalExpr (('*'|'/') terminalExpr)*; terminalExpr -> integer | variable | '(' expr ')'; But the associativity is ambiguous. Here's a more

What are Context-Free Grammars and Backus Naur Form?

廉价感情. 提交于 2020-01-13 13:08:52
问题 Can someone please explain in layman's terms: what a context-free grammar is? what Backus Naur Form is? How to use this notation? How to do string derivation? How to describe language syntax? 回答1: A context-free grammar (CFG) G is a quadruple (V, Σ, R, S) where V: a set of non-terminal symbols Σ: a set of terminals (V ∩ Σ = Ǿ) R: a set of rules (R: V → (V U Σ)*) S: a start symbol Example of CFG: V = {q, f,} Σ = {0, 1} R = {q → 11q, q → 00f, f → 11f, f → ε} S=q (R= {q → 11q | 00f, f → 11f | ε