grammar

What trick does Java use to avoid spaces in >>?

戏子无情 提交于 2019-12-29 05:07:11
问题 In the Java Generic Book, while contrasting the difference between C++ Templates and Java Generic says: In C++, a problem arises because >> without the space denotes the right-shift operator. Java fixes the problem by a trick in the grammar.) What is this trick? 回答1: This is actually being fixed in C++ in the next version. There really isn't much of a trick; if you encounter >> while in the process of parsing a generic or template where instead you expected >, then you already have enough

AST and operator precedence in rule definition

≡放荡痞女 提交于 2019-12-28 19:29:12
问题 Hello [¹] I have a simple parser (see below). It intends to parse conditional expressions (relational arithmetic operations and logic combinations thereof). In the example given there, it parses successfully A>5 but then stops and ignores the rest of the input, and this is consistent with my impl. How do I change the expr_ rule to make it parse the entire input? #include <cstdint> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix.hpp> #include <boost/spirit/include

Is C++ context-free or context-sensitive?

[亡魂溺海] 提交于 2019-12-28 03:05:29
问题 I often hear claims that C++ is a context-sensitive language. Take the following example: a b(c); Is this a variable definition or a function declaration? That depends on the meaning of the symbol c . If c is a variable , then a b(c); defines a variable named b of type a . It is directly initialized with c . But if c is a type , then a b(c); declares a function named b that takes a c and returns an a . If you look up the definition of context-free languages, it will basically tell you that

Source for parsing C grammar using JavaCC

大憨熊 提交于 2019-12-25 10:01:05
问题 As an project assignment, I need to parse a plain-C grammar from Java to generate AST output. As a startup, I am using the file c.jj that I have found among grammar files at http://java.net/projects/javacc/sources/svn/ but I found that it only has syntactic and lexical actions and no real semantics for parsing C source. Is there some other source that incorporate typedef, variables, construct functions, include files? 回答1: You could go looking for a complete grammar. Will you learn much this

Source for parsing C grammar using JavaCC

╄→гoц情女王★ 提交于 2019-12-25 09:59:28
问题 As an project assignment, I need to parse a plain-C grammar from Java to generate AST output. As a startup, I am using the file c.jj that I have found among grammar files at http://java.net/projects/javacc/sources/svn/ but I found that it only has syntactic and lexical actions and no real semantics for parsing C source. Is there some other source that incorporate typedef, variables, construct functions, include files? 回答1: You could go looking for a complete grammar. Will you learn much this

How to remove ambiguity in the following grammar?

一曲冷凌霜 提交于 2019-12-25 08:45:06
问题 How to remove ambiguity in following grammar? E -> E * F | F + E | F F -> F - F | id 回答1: First, we need to find the ambiguity. Consider the rules for E without F; change F to f and consider it a terminal symbol. Then the grammar E -> E * f E -> f + E E -> f is ambiguous. Consider f + f * f: E E | | +-------+--+ +-+-+ | | | | | | E * f f + E +-+-+ | | | | +-+-+ f + E E * f | | f f We can resolve this ambiguity by forcing * or + to take precedence. Typically, * takes precedence in the order of

Shift/Reduce conflict in CUP

我是研究僧i 提交于 2019-12-24 20:11:17
问题 I'm trying to write a parser for a javascript-ish language with JFlex and Cup, but I'm having some issues with those deadly shift/reduce problems and reduce/reduce problems. I have searched thoroughly and have found tons of examples, but I'm not able to extrapolate these to my grammar. My understanding so far is that these problems are because the parser cannot decide which way it should take because it can't distinguish. My grammar is the following one: start with INPUT; INPUT::= PROGRAM;

Handling line feed in ANTLR4 grammar with Python target

青春壹個敷衍的年華 提交于 2019-12-24 19:44:35
问题 I am working on an ANTLR4 grammar for parsing Python DSL scripts (a subset of Python, basically) with the target set as the Python 3 . I am having difficulties handling the line feed. In my grammar, I use lexer::members and NEWLINE embedded code based on Bart Kiers's Python3 grammar for ANTLR4 which are ported to Python so that they can be used with Python 3 runtime for ANTLR instead of Java. My grammar differs from the one provided by Bart (which is almost the same used in the Python 3 spec)

Microsoft Speech Platform: recognize word repetitions

99封情书 提交于 2019-12-24 14:34:52
问题 I use Microsoft Speech Platform to recognize speech at output it on screen. But, i have problem: for example, i have grammar (constructs by GrammarBuilder and Choices - "red","green","black") When i say- "red green black"- i can get only "red", maybe "red green" , but not "red green black". Some code: Thread.CurrentThread.CurrentCulture = new CultureInfo("ru-RU"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU"); // Create a new SpeechRecognitionEngine instance. _sre = new

Attribute access on int literals

倖福魔咒の 提交于 2019-12-24 13:26:17
问题 >>> 1 .__hash__() 1 >>> 1.__hash__() File "<stdin>", line 1 1.__hash__() ^ SyntaxError: invalid syntax It has been covered here before that the second example doesn't work because the int literal is actually parsed as a float. My question is, why doesn't python parse this as attribute access on an int, when the interpretation as a float is a syntax error? The docs section on lexical analysis seem to suggest whitespace only required when other interpretations are ambiguous, but perhaps I'm