grammar

How to enumerate the strings of a context-free grammar?

点点圈 提交于 2019-12-06 03:59:39
问题 What algorithm do you use to enumerate the strings generated by a context-free grammar? It seems doable when there is no recursion, but I can't figure out how to do it in the general case, which might contain all kinds of (possibly indirect) recursion. (I'm not looking for an esoteric solution like the one on this page; I'm looking for an algorithm that I could map to standard imperative code.) 回答1: Here's an obvious but somewhat inefficient algorithm: Construct R, the Earley parser for the

C++: difference between 0. and 0.0?

∥☆過路亽.° 提交于 2019-12-06 03:52:10
I am well aware of the difference between 0 and 0.0 (int and double). But is there any difference between 0. and 0.0 ( please note the . )? Thanks a lot in advance, Axel There is no difference. Both literals are double. From the C++-Grammar: fractional-constant: digit-sequenceopt . digit-sequence digit-sequence . See: Hyperlinked C++ BNF Grammar No, there is not. No. You can also write .0 as far as I know. Just having the . as part of the number identifies it as a floating point type. This: cout << (5 / 2) << endl; cout << (5. / 2) << endl; cout << (5.0 / 2) << endl; Prints this: 2 2.5 2.5 You

Is Python 3.5's grammar LL(1)?

扶醉桌前 提交于 2019-12-06 02:41:55
问题 I saw http://matt.might.net/teaching/compilers/spring-2015/ saying Python 3.4 is LL(1) Is Python 3.5's grammar still LL(1) so one can write a recursive descent parser? 回答1: Yes. This is a deliberate language feature, and not just something that happened to be the case. PEP 3099 explicitly rejected any changes to this for the Python 2 -> 3 transition (a notably bigger transition than any 3.x -> 3.y will be): The parser won't be more complex than LL(1). Simple is better than complex. This idea

Grammar: difference between a top down and bottom up?

此生再无相见时 提交于 2019-12-06 02:19:17
问题 What is the difference between a top down and bottom up grammar? An example would be awesome. 回答1: First of all, the grammar itself isn't top-down or bottom-up, the parser is (though there are grammars that can be parsed by one but not the other). From a practical viewpoint, the main difference is that most hand-written parsers are top-down, while a much larger percentage of machine-generated parsers are bottom-up (though, of course, the reverse is certainly possible). A top-down parser

String Template: make all variable declaration global

十年热恋 提交于 2019-12-06 02:17:00
I am trying to implement a translator using ANTLR+StringTemplate. I have a starting language that is java like and multiple destination language. I used the example: http://www.antlr.org/wiki/display/ST/Language+Translation+Using+ANTLR+and+StringTemplate One of my destination language needs all variables to be declared globally. I wrote a grammar that recognizes variables, but i cannot find e way in my template for making a local variable to be declared globally. Of course if I would have just one translation I would be able to do it, but I have multiple translation and some of them have local

Removing left recursion in DCG - Prolog

自闭症网瘾萝莉.ら 提交于 2019-12-06 02:14:01
问题 I've got a small problem with left recursion in this grammar. I'm trying to write it in Prolog, but I don't know how to remove left recursion. <expression> -> <simple_expression> <simple_expression> -> <simple_expression> <binary_operator> <simple_expression> <simple_expression> -> <function> <function> -> <function> <atom> <function> -> <atom> <atom> -> <number> | <variable> <binary_operator> -> + | - | * | / expression(Expr) --> simple_expression(SExpr), { Expr = SExpr }. simple_expression

Parse tree and grammar information

橙三吉。 提交于 2019-12-06 02:12:26
问题 Does anyone know where to find good online resources with examples of how to make grammars and parse trees? Preferably introductory materials. Info that is n00b friendly, haven't found anything good with Google myself. Edit: I'm thinking about theory, not a specific parser software. 回答1: Not online, but maybe you should take a look at Compilers: Principles, Techniques, and Tools (2nd Edition) by Aho et al. This is a standard text that has been evolving for 30 years (if you count the 1st

BibTex grammar for ANTLR

廉价感情. 提交于 2019-12-06 01:12:03
问题 I'm looking for a bibtex grammar in ANTLR to use in a hobby project. I don't want to spend my time for writing ANTLR grammar (this may take some time for me because it will involve a learning curve). So I'd appreciate for any pointers. Note: I've found bibtex grammars for bison and yacc but couldn't find any for antlr. Edit: As Bart pointed the I don't need to parse the preambles and tex in the quoted strings. 回答1: Here's a (very) rudimentary BibTex grammar that emits an AST (contrary to a

How to resolve a shift/reduce conflict forcing a shift or a reduce?

别等时光非礼了梦想. 提交于 2019-12-06 01:08:39
When there is a shift/reduce conflict in Yacc/Bison, is it possible to force the conflict to be solved exactly as you want? In other words: is it possible explicitly force it to prioritize the shift or the reduce? For what I have read, if you are happy with the default resolution you can tell the generator to not complain about it . I really don't like this because it is obfuscating your rational choice. Another option is to rewrite the grammar to fix the issue. I don't know if this is always possible and often this makes it much harder to understand. Finally, I have read the precedence rules

XText: use custom terminals definitions

点点圈 提交于 2019-12-06 00:48:15
I'm totally new to XText. When you define a grammar using XText you could specify a second grammar and use the definitions it declares as it is said here : grammar org.eclipse.xtext.example.Domainmodel with org.eclipse.xtext.common.Terminals In Xtext each grammar has a unique name, which like public Java classes needs to reflect the location of the file within the Java classpath. In our case the grammar file is located in /org/eclipse/xtext/example/Domainmodel.xtext therefore the name of the grammar is org.eclipse.xtext.example.Domainmodel. The second part of that statement ( with org.eclipse