grammar

ANTLR generating empty conditions

一笑奈何 提交于 2019-12-24 02:26:10
问题 I'm trying to learn to use ANTLR, but I cannot figure out what's wrong with my code in this case. I hope this will be really easy for anyone with some experience with it. This is the grammar (really short). grammar SmallTest; @header { package parseTest; import java.util.ArrayList; } prog returns [ArrayList<ArrayList<String>> all] :(stat { if ($all == null) $all = new ArrayList<ArrayList<String>>(); $all.add($stat.res); } )+ ; stat returns [ArrayList<String> res] :(element { if ($res == null)

Migration tool for ANTLR grammar

蹲街弑〆低调 提交于 2019-12-24 01:54:44
问题 Suppose I have a following simple grammar (query DSL): grammar TestGrammar; term : textTerm ; textTerm : 'Text' '(' T_VALUE '=' STRING+ ')' ; T_VALUE : 'value' ; STRING : '"' .+? '"' ; WS : [ \t\r\n]+ -> skip ; Then at some point I decide that text term format needs to be changed, for example: Text(value = "123") -> MyText(val = "123") How should I approach migrating existing data that users have generated with previous version of grammar? 回答1: Assumption Let's make one simplification of your

Looking for a little help and guidance on creating a grammar for JS/CC

廉价感情. 提交于 2019-12-24 01:51:33
问题 In regards to my last question How would I write an interpreter for this in javascript? I'm attempting to create a JS/CC grammar. They have a live install of the program if you want to try my grammar out, just copy and paste it into the top box, click Build and then Run . The result should run the little program at the bottom of the grammar, which is: popup 'String Literal' popup 42 set myVariable to 10 popup myVariable When run you should get 3 alerts, the last one should be 10, but its 0.

Jison: Reduce Conflict where actually no conflict is

我的梦境 提交于 2019-12-24 00:57:02
问题 I'm trying to generate a small JavaScript parser which also includes typed variables for a small project. Luckily, jison already provides a jscore.js which I just adjusted to fit my needs. After adding types I ran into a reduce conflict. I minimized to problem to this minimum JISON: Jison: %start SourceElements %% // This is up to become more complex soon Type : VAR | IDENT ; // Can be a list of statements SourceElements : Statement | SourceElements Statement ; // Either be a declaration or

Why can I construct a string with multiple string literals? [duplicate]

不问归期 提交于 2019-12-23 19:06:05
问题 This question already has answers here : Why allow concatenation of string literals? (10 answers) Closed 5 years ago . #include <iostream> #include <string> int main() { std::string str = "hello " "world" "!"; std::cout << str; } The following compiles, runs, and prints: hello world! see live It seems as though the string literals are being concatenated together, but interestingly this can not be done with operator + : #include <iostream> #include <string> int main() { std::string str =

Applying a (possibly unary) function recursively onto itself

倖福魔咒の 提交于 2019-12-23 18:44:06
问题 I am trying to express an L-system in Haskell https://en.m.wikipedia.org/wiki/L-system, specifically Lindenmayer's original L-system for modelling the growth of algae. variables : A B constants : none axiom : A rules : (A → AB), (B → A) For me the natural way to approach this problem is to apply the rules to each element in the list, which (to me) means that I could model the solution using some type of string substitution. Example: For the list of "characters" [A, B, A we'd apply the rules

Conversion to Chomsky Normal Form

我的未来我决定 提交于 2019-12-23 18:34:09
问题 I do need your help. I have these productions: 1) A--> aAb 2) A--> bAa 3) A--> ε I should apply the Chomsky Normal Form (CNF). In order to apply the above rule I should: eliminate ε producions eliminate unitary productions remove useless symbols Immediately I get stuck. The reason is that A is a nullable symbol (ε is part of its body) Of course I can't remove the A symbol. Can anyone help me to get the final solution? 回答1: As the Wikipedia notes, there are two definitions of Chomsky Normal

Decompose expression in base operation: ANTLR + StringTemplate

流过昼夜 提交于 2019-12-23 17:30:32
问题 I am trying to write a translator for a Java like language to multiple languages. Right now I am facing 2 problems: First is to decompose complex expression in a sequence of basic operation and then translating them to destination language. For example my starting language can be: var a = (ln(b) + avg(c))*2 I'd like to traslate it like: var x1 = log_N(b); var x2 = average(c); var x3 = sum(x1, x2); var a = multiply(x3, 2); I think i have to use a Tree parser, nut I am not sure how to integrate

JAPE rule Sentence contains multiple cases

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 16:34:41
问题 How can i check whether a sentence contain combinations? For example consider sentence. John appointed as new CEO for google. I need to write a rule to check whether sentence contains < 'new' + 'Jobtitle' >. How can i achieve this. I tried following. I need to check is there 'new' before word . Rule: CustomRules ( { Sentence contains {Lookup.majorType == "organization"}, Sentence contains {Lookup.majorType == "jobtitle"}, Sentence contains {Lookup.majorType == "person_first"} } ) 回答1: One way

Is there a BNF with arguments for non-terminal symbols?

会有一股神秘感。 提交于 2019-12-23 13:05:36
问题 In working with Prolog DCG to parse input it is nice to have an accompaning BNF of the grammar. For example: BNF <Sentence> ::= <Noun_phrase> <Verb_phrase> <Noun_phrase> ::= <Determiner> <Noun> <Verb_phrase> ::= <Verb> <Phrase> <Determiner> ::= a <Determiner> ::= the <Noun> ::= cat <Noun> ::= mouse <Verb> ::= scares <Verb> ::= hates as Prolog DCG sentence --> noun_phrase, verb_phrase. verb_phrase --> verb, noun_phrase. noun_phrase --> determiner, noun. determiner --> [a]. determiner --> [the]