left-recursion

Parse function call with PyParsing

妖精的绣舞 提交于 2020-01-23 10:47:12
问题 I'm trying to parse a simple language. The trouble comes with parsing function calls. I'm trying to tell it that a function call is an expression followed by left parenthesis, argument list, and right parenthesis. I have something like this: expr = Forward() iden = Word(alphas+'_', alphanums+'_') integer = Word(nums) binop = operatorPrecedence(expr, ...) # irrevelant call = expr + Literal('(') + delimitedList(expr) + Literal(')') expr << call | integer | iden The problem is obvious: expr is

Step by step elimination of this indirect left recursion

♀尐吖头ヾ 提交于 2019-12-31 13:15:02
问题 I've seen this algorithm one should be able to use to remove all left recursion. Yet I'm running into problems with this particular grammar: A -> Cd B -> Ce C -> A | B | f Whatever I try I end up in loops or with a grammar that is still indirect left recursive. What are the steps to properly implement this algorithm on this grammar? 回答1: Rule is that you first establish some kind of order for non-terminals, and then find all paths where indirect recursion happens. In this case order would be

Overcoming infinite left recursion in TextX parser

被刻印的时光 ゝ 提交于 2019-12-25 00:48:58
问题 I am writing a parser for an existing language, using the TextX Python Library (based on the Arpeggio PEG parser) But when I try to use it to parse a file, I get the exception: RecursionError: maximum recursion depth exceeded while calling a Python object Here is a minimal example that raises this exception: #!/usr/bin/env python from textx import metamodel_from_str meta_model_string = "Expr: ( Expr '+' Expr ) | INT ;" model_string = "1 + 1" mm = metamodel_from_str(meta_model_string, debug

Stack overflow when using parser combinators

随声附和 提交于 2019-12-24 12:01:13
问题 import scala.util.parsing.combinator._ object ExprParser extends JavaTokenParsers { lazy val name: Parser[_] = "a" ~ rep("a" | "1") | function_call lazy val function_call = name ~ "(" ~> name <~ ")" } recurs indefinitely for function_call.parseAll("aaa(1)") . Obviously, it is because 1 cannot inter the name and name enters the function_call , which tries the name, which enters the funciton call. How do you resolve such situations? There was a solution to reduce name to simple identifier def

Removing Left Recursion in a Context Free Grammar

流过昼夜 提交于 2019-12-12 15:02:03
问题 Trying to figure out removing left recursion in context free grammars. I'm used to certain forms, but this one has me a bit boggled. S --> S {S} S | (A) | a A --> {S} A | epsilon I also have to design a decent parser, which I can do. However, figuring out this left recursion (especially on the first one) has me confused. 回答1: There is an interesting Wikipedia article on left-recursion. It also has a section about removing left-recursion for non-context grammars. http://en.wikipedia.org/wiki

ANTLR4 Mutual left recursion

痞子三分冷 提交于 2019-12-11 01:08:49
问题 I just ran into a strange problem with ANTLR 4.2.2: Consider a (simplified) java grammar. This does not compile: classOrInterfaceType : (classOrInterfaceType) '.' Identifier | Identifier ; ANTLR outputs the following error: error(119): Java.g4::: The following sets of rules are mutually left-recursive [classOrInterfaceType] Yes, I also see a left recursion. But I do not see a mutual left recursion, only a usual one. When I remove the parenthesis around (classOrInterfaceType) , then it

Mutual Left Recursion ANTLR 4

荒凉一梦 提交于 2019-12-11 00:38:30
问题 I'm sorry to ask yet another question on mutual left recursion, I feel like mine is unique to my situation, or at least I can't figure out enough to relate it to everyone else's grammars. I'm a bit new to the comp sci world (I'm self taught in java, which is my target language, and now ANTLR4) so if possible please describe things in layperson terms, not CS major terms. I'm writing a program that requires algebra and symbolic derivatives, and of course that requires that things be parsed, and

DCG and left recursion

天大地大妈咪最大 提交于 2019-12-10 22:55:40
问题 I am trying to implement a dcg that takes a set of strings of the form {a,b,c,d}*.The problem i have is if I have a query of the form s([a,c,b],[]),It returns true which is the right answer but when i have a query of the form s([a,c,f],[]),It does not return an answer and it runs out of local stack. s --> []. s --> s,num. num --> [a]. num--> [b]. num--> [c]. num--> [d]. 回答1: Use phrase/2 Let's try phrase(s,[a,b,c]) in place of s([a,b,c],[]) . The reason is very simple: In this manner we are

Left Factoring & Removing Left Recursion JavaCC

一世执手 提交于 2019-12-08 01:30:21
问题 I have a grammar which I have to use JJTree and JavaCC to create a symbol table and an AST. While I fully understand the sections of my assignment to create the table and tree, the grammar I was given is ambiguous, contains left recursion and indirect left recusion. It also needs to be left factored. I have trawled all over the internet to try find methods that would work for me. For example: A ::= Aα | β can be changed to: A ::= βA' A' ::= αA' | ε But I don't know how to apply this to my

How to avoid mutual left-recursion in ANTLR 4

血红的双手。 提交于 2019-12-07 04:50:01
问题 I am writing a grammar to handle scalar and vector expressions. The grammar below is simplified to show the problem I have where a scalar expression can be derived from a vector and a vector can be derived from a scalar. For example, a vector could be a literal [1, 2, 3] or the product of a scalar and a vector 2 * [1, 2, 3] (equivalent to [2, 4, 6] ). A scalar could be a literal 2 or an index into a vector [1, 2, 3][1] (equivalent to 2 ). grammar LeftRecursion; Integer : [0-9]+ ; WhiteSpace :