grammar

yacc - How to do a if condition

你离开我真会死。 提交于 2020-01-16 02:04:06
问题 I am trying to do a simple if condition from an input file. i will have something like if(color = black) No matter what i do i keep getting 1 shift / reduce I am very new to lex and yacc Do YACC grammars often have shift-reduce conflicts? and should i not worry about them? My lex file will return every character in the file correctly so i wont show you the lex file However, here is my yacc file: %{ #include <ctype.h> #include <stdio.h> %} |IF LPAREN COLOR EQ BLACK RPAREN {$$ = $1; printf(

How to implement a negative LOOKAHEAD check for a token in JavaCC?

旧时模样 提交于 2020-01-15 19:16:42
问题 I currently implementing a JavaScript/ECMAScript 5.1 parser with JavaCC. I recently learned about LOOKAHEAD s which are handy here as the grammar is not fully LL(1). One of the things I see in the ECMAScript grammar is "negative lookahead check", like in the following ExpressionStatement production: ExpressionStatement : [lookahead ∉ {{, function}] Expression ; So I'll probably need something like LOOKAHEAD(!("{" | "function")) but it does not work in this syntax. My question is, how could I

ANTLR How to use lexer rules having same starting?

走远了吗. 提交于 2020-01-14 09:53:29
问题 How to use lexer rules having same starting? I am trying to use two similar lexer rules (having the same starting): TIMECONSTANT: ('0'..'9')+ ':' ('0'..'9')+; INTEGER : ('0'..'9')+; COLON : ':'; Here is my sample grammar: grammar TestTime; text : (timeexpr | caseblock)*; timeexpr : TIME; caseblock : INT COLON ID; TIME : ('0'..'9')+ ':' ('0'..'9')+; INT : ('0'..'9')+; COLON : ':'; ID : ('a'..'z')+; WS : (' ' | '\t' | '\r' | '\n') {$channel=HIDDEN;}; When i try to parse text: 12:44 123 : abc

Find a grammar of binary number divisible by 5 with 1 as MSB

青春壹個敷衍的年華 提交于 2020-01-14 03:41:08
问题 How can I find a grammar of binary number divisible by 5 with 1 as MSB and find the reversal of L So, I need a grammar that generates numbers like.. 5 = 101 10 = 1010 15 = 1111 20 = 10100 25 = 110011 and so on 回答1: I'm assuming this is homework and you just want a hint. Let's consider a somewhat similar question, but in base 10. How can we write a CFG for numbers divisible by 3. At first glance, this seems unlikely, but it's actually pretty simple. We start with the observation that: 10 k ≅ 1

How to tell the precedence of operators in a context free grammar

♀尐吖头ヾ 提交于 2020-01-13 19:34:09
问题 How can we know which of the following logical operations ( or, and, not ) in the bellow context free grammar have higher precedence? Is there a general approach to this kind of problems? X → X or Y | Y Y → Y and Z | Z Z → not Z | (X) | true | false 回答1: Here's an example approach: expr -> addExpr; addExpr -> multExpr (('+'|'-') multExpr)*; multExpr -> terminalExpr (('*'|'/') terminalExpr)*; terminalExpr -> integer | variable | '(' expr ')'; But the associativity is ambiguous. Here's a more

How to debug an invalid ParseKit Grammar?

自闭症网瘾萝莉.ら 提交于 2020-01-13 19:32:07
问题 I am trying to write a grammar for ParseKit so that it matches basic propositional logic sentences in an iphone app. Can someone please tell me where im going wrong. @start = wff; wff = disjunction (implies disjunction)?; disjunction = conjuction (or conjuction)*; conjunction = notExpression (and notExpression)*; notExpression = (not | not primaryExpression); primaryExpression = variable | lbracket wff rbracket; variable = p | q | r; p = 'P'; q = 'Q'; r = 'R'; implies = '→'; and = '∧'; or = '

How to debug an invalid ParseKit Grammar?

為{幸葍}努か 提交于 2020-01-13 19:32:05
问题 I am trying to write a grammar for ParseKit so that it matches basic propositional logic sentences in an iphone app. Can someone please tell me where im going wrong. @start = wff; wff = disjunction (implies disjunction)?; disjunction = conjuction (or conjuction)*; conjunction = notExpression (and notExpression)*; notExpression = (not | not primaryExpression); primaryExpression = variable | lbracket wff rbracket; variable = p | q | r; p = 'P'; q = 'Q'; r = 'R'; implies = '→'; and = '∧'; or = '

Grammar: difference between a top down and bottom up? (Example)

Deadly 提交于 2020-01-13 09:14:28
问题 This is a follow up question from Grammar: difference between a top down and bottom up? I understand from that question that: the grammar itself isn't top-down or bottom-up, the parser is there are grammars that can be parsed by one but not the other (thanks Jerry Coffin So for this grammar (all possible mathematical formulas): E -> E T E E -> (E) E -> D T -> + | - | * | / D -> 0 D -> L G G -> G G G -> 0 | L L -> 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Would this be readable by a top down and

Verify correct use of “a” and “an” in English texts - Python [closed]

99封情书 提交于 2020-01-12 05:53:11
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 6 years ago . I want to create a program that reads text from a file and points out when "a" and "an" is used incorrect. The general rule as far as I know is that "an" is used when the next words starts with a vowel. But it should also take into consideration that there are exceptions which

ANTLR Is it possible to make grammar with embed grammar inside?

ⅰ亾dé卋堺 提交于 2020-01-11 05:26:07
问题 ANTLR: Is it possible to make grammar with embed grammar (with it's own lexer) inside? For example in my language I have ability to use embed SQL language: var Query = [select * from table]; with Query do something ....; Is it possible with ANTLR? 回答1: Is it possible to make grammar with embed grammar (with it's own lexer) inside? If you mean whether it is possible to define two languages in a single grammar (using separate lexers), then the answer is: no, that's not possible. However, if the