context-free-grammar

What are the differences between PEGs and CFGs?

梦想与她 提交于 2019-12-03 02:16:32
问题 From this wikipedia page: The fundamental difference between context-free grammars and parsing expression grammars is that the PEG's choice operator is ordered. If the first alternative succeeds, the second alternative is ignored. Thus ordered choice is not commutative, unlike unordered choice as in context-free grammars and regular expressions. Ordered choice is analogous to soft cut operators available in some logic programming languages. Why does PEG's choice operator short circuits the

Is D's grammar really context-free?

南笙酒味 提交于 2019-12-03 01:02:19
问题 I've posted this on the D newsgroup some months ago, but for some reason, the answer never really convinced me, so I thought I'd ask it here. The grammar of D is apparently context-free. The grammar of C++, however, isn't (even without macros). ( Please read this carefully! ) Now granted, I know nothing (officially) about compilers, lexers, and parsers. All I know is from what I've learned on the web. And here is what (I believe) I have understood regarding context, in not-so-technical lingo:

Step by step elimination of this indirect left recursion

我怕爱的太早我们不能终老 提交于 2019-12-02 23:28:26
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? 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 A < B < C, and possible paths for recursion of non-terminal C would be C=> A => Cd and C=> B => Ce so new

Context-free grammar for C

我是研究僧i 提交于 2019-12-02 22:44:34
I'm working on a parser for C. I'm trying to find a list of all of the context-free derivations for C. Ideally it would be in BNF or similar. I'm sure such a thing is out there, but googling around hasn't given me much. Reading the source code for existing parsers/compilers has proven to be far more confusing than helpful, as most that I've found are much more ambitious and complicated than the one I'm building. You could always use Annex A of the C11 standard itself. The freely available draft standard will work for your purposes, at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf .

chomsky hierarchy in plain english

China☆狼群 提交于 2019-12-02 17:30:35
I'm trying to find a plain (i.e. non-formal) explanation of the 4 levels of formal grammars (unrestricted, context-sensitive, context-free, regular) as set out by Chomsky. It's been an age since I studied formal grammars, and the various definitions are now confusing for me to visualize. To be clear, I'm not looking for the formal definitions you'll find everywhere (e.g. here and here -- I can google as well as anyone else), or really even formal definitions of any sort. Instead, what I was hoping to find was clean and simple explanations that don't sacrifice clarity for the sake of

How to define a grammar for a programming language

隐身守侯 提交于 2019-12-02 17:09:48
How to define a grammar (context-free) for a new programming language (imperative programming language) that you want to design from scratch. In other words: How do you proceed when you want to create a new programming language from scratch. One step at a time. No seriously, start with expressions and operators, work upwards to statements, then to functions/classes etc. Keep a list of what punctuation is used for what. In parallel define syntax for referring to variables, arrays, hashes, number literals, string literals, other builtin literal. Also in parallel define your data naming model and

How do Java, C++, C#, etc. get around this particular syntactic ambiguity with < and >?

末鹿安然 提交于 2019-12-02 16:39:04
I used to think C++ was the "weird" one with all the ambiguities with < and > , but after trying to implement a parser I think I found an example which breaks just about every language that uses < and > for generic types: f(g<h, i>(j)); This could be syntactically either interpreted as a generic method call ( g ), or it could be interpreted as giving f the results of two comparisons. How do such languages (especially Java, which I thought was supposed to be LALR(1)-parsable? ) get around this syntactic ambiguity? I just can't imagine any non-hacky/context-free way of dealing with this, and I'm

What are the differences between PEGs and CFGs?

馋奶兔 提交于 2019-12-02 15:48:31
From this wikipedia page: The fundamental difference between context-free grammars and parsing expression grammars is that the PEG's choice operator is ordered. If the first alternative succeeds, the second alternative is ignored. Thus ordered choice is not commutative, unlike unordered choice as in context-free grammars and regular expressions. Ordered choice is analogous to soft cut operators available in some logic programming languages. Why does PEG's choice operator short circuits the matching? Is it because to minimize memory usage (due to memoization)? I'm not sure what the choice

Is D's grammar really context-free?

微笑、不失礼 提交于 2019-12-02 14:21:38
I've posted this on the D newsgroup some months ago, but for some reason, the answer never really convinced me, so I thought I'd ask it here. The grammar of D is apparently context-free . The grammar of C++, however, isn't (even without macros). ( Please read this carefully! ) Now granted, I know nothing (officially) about compilers, lexers, and parsers. All I know is from what I've learned on the web. And here is what (I believe) I have understood regarding context, in not-so-technical lingo: The grammar of a language is context-free if and only if you can always understand the meaning

Finding a grammar is not LL(1) without using classical methods and transforming it to LL(1)

烈酒焚心 提交于 2019-12-02 10:22:49
Let's say i have this grammar: S -> A C x | u B A A -> z A y | S u | ε B -> C x | y B u C -> B w B | w A This grammar is obviously not LL(1), which i can find constructing the parsing table. But is there any way i can prove that this grammar is not LL(1) without using the classical methods i.e. without constructing the parsing table or finding any conflicts? Also how can i convert this grammar to LL(1)? I think i have to use both epsilon-derivation elimination and left recursion elimination but its a bit tricky and as many times i've tried i couldn't transform it to LL(1). Thank you in advance