context-free-grammar

Parsing a context-free grammar in Python

人走茶凉 提交于 2019-11-30 03:34:59
问题 What tools are available in Python to assist in parsing a context-free grammar? Of course it is possible to roll my own, but I am looking for a generic tool that can generate a parser for a given CFG. 回答1: I warmly recommend PLY - it's a Lex/Yacc clone in Python that uses the language's introspection facilities in a sophisticated manner to allow for a very natural specification of the grammar. Yacc, if you recall, is the very embodiment of CFGs in an understandable DSL that defines how one

Converting ambiguous grammar to unambiguous

拈花ヽ惹草 提交于 2019-11-30 03:30:35
I did not understand how a unambiguous grammar is derived from a ambiguous grammar? Consider the example on site: Example . How was the grammar derived is confusing to me. Can anyone please guide me ? The example has two grammars: Ambiguous: E → E + E | E ∗ E | (E) | a Unambiguous: E → E + T | T T → T ∗ F | F F → (E) | a The unambiguous grammar was derived from the ambiguous one using information not specified in the ambiguous grammar: The operator '*' binds tighter than the operator '+'. Both the operators '*' and '+' are left associative. Without the external information, there is no way to

How can I determine if a language is context free or not?

北城以北 提交于 2019-11-30 02:02:41
How can I know whether the languages are context free or not? P Shved First, you should attempt to build a context-free grammar that forms the language in subject. A grammar is context-free if left-hand sides of all productions contain exactly one non-terminal symbol. By definition, if one exists, then the language is context-free. An equivalent construct would be a pushdown automaton . It's the same as DFA, but with a stack available. It may be easier to build than a grammar. However, if you fail to build a grammar or an automaton, it doesn't mean that a language is not context-free; perhaps,

Context-free grammars versus context-sensitive grammars?

怎甘沉沦 提交于 2019-11-29 19:14:24
Can someone explain to me why grammars [context-free grammar and context-sensitive grammar] of this kind accepts a String? What I know is Context-free grammar is a formal grammar in which every production(rewrite) rule is a form of V→w Where V is a single nonterminal symbol and w is a string of terminals and/or non-terminals. w can be empty Context-sensitive grammar is a formal grammar in which left-hand sides and right hand sides of any production (rewrite) rules may be surrounded by a context of terminal and nonterminal symbols. But how can i explain why these grammar accepts a String? An

Difference between an LL and Recursive Descent parser?

房东的猫 提交于 2019-11-29 18:41:36
I've recently being trying to teach myself how parsers (for languages/context-free grammars) work, and most of it seems to be making sense, except for one thing. I'm focusing my attention in particular on LL(k) grammars , for which the two main algorithms seem to be the LL parser (using stack/parse table) and the Recursive Descent parser (simply using recursion). As far as I can see, the recursive descent algorithm works on all LL(k) grammars and possibly more, whereas an LL parser works on all LL(k) grammars. A recursive descent parser is clearly much simpler than an LL parser to implement,

Is “regex” in modern programming languages really “context sensitive grammar”?

怎甘沉沦 提交于 2019-11-29 06:45:33
Over the years, "regex" pattern matching has been getting more and more powerful to the point where I wonder: is it really just context-sensitive-grammar matching? Is it a variation/extension of context-free-grammar matching? Where is it right now and why don't we just call it that instead of the old, restrictive "regular expression"? In particular backreferences to capturing parentheses make regular expressions more complex than regular, context-free, or context-sensitive grammars. The name is simply historically grown (as many words). See also this section in Wikipedia and this explanation

How can I determine if a language is context free or not?

本秂侑毒 提交于 2019-11-28 22:26:06
问题 How can I know whether the languages are context free or not? 回答1: First, you should attempt to build a context-free grammar that forms the language in subject. A grammar is context-free if left-hand sides of all productions contain exactly one non-terminal symbol. By definition, if one exists, then the language is context-free. An equivalent construct would be a pushdown automaton. It's the same as DFA, but with a stack available. It may be easier to build than a grammar. However, if you

Is there a fast algorithm to determine the godel number of a term of a context free language?

℡╲_俬逩灬. 提交于 2019-11-28 20:47:02
Suppose we have a simple grammar specification. There is a way to enumerate terms of that grammar that guarantees that any finite term will have a finite position, by iterating it diagonally . For example, for the following grammar: S ::= add add ::= mul | add + mul mul ::= term | mul * term term ::= number | ( S ) number ::= digit | digit number digit ::= 0 | 1 | ... | 9 You can enumerate terms like that: 0 1 0+0 0*0 0+1 (0) 1+0 0*1 0+0*0 00 ... etc My question is: is there a way to do the opposite? That is, to take a valid term of that grammar, say, 0+0*0 , and find its position on such

How to find FIRST and FOLLOW sets of a recursive grammar?

主宰稳场 提交于 2019-11-28 18:29:45
Suppose I have the following CFG. A -> B | Cx | EPSILON B -> C | yA C -> B | w | z Now if I try to find FIRST(C) = FIRST(B) U FIRST(w) U FIRST(z) = FIRST(C) U FIRST(yA) U {w, z} That is, I'm going in a loop. Thus I assume I have to convert it into a form which has immediate left recursion, which I can do as follows. A -> B | Cx | EPSILON B -> C | yA C -> C | yA | w | z Now if I try to calculate FIRST sets, I think I can get it done as follows. FIRST(C) = FIRST(C) U FIRST(yA) U FIRST(w) U FIRST(z) = { y, w, z } // I ignore FIRST(C) FIRST(B) = FIRST(C) U FIRST(yA) = { y, w, z } FIRST(A) = FIRST

Is there a standard C++ grammar?

浪尽此生 提交于 2019-11-28 16:52:27
Does the standard specify the official C++ grammar? I searched, but did not find it anywhere. Also, I wish to read a bit about C++ grammar in detail, like which category of grammars it falls in, etc. Any links pointing me in the right direction would be helpful. By category, I mean taken from here . James McNellis Yes, it does. The grammar is described in detail throughout the standard and is summarized in Appendix A: Grammar Summary (it's Appendix A in both the C++03 standard and the C++0x final committee draft). You can purchase the C++03 standard or you can download the C++0x FCD (it's