grammar

Tips for creating “Context Free Grammar”

人盡茶涼 提交于 2019-12-17 06:31:14
问题 I am new to CFG's, Can someone give me tips in creating CFG that generates some language For example L = {a m b n | m >= n} What I got is: S o -> a | aS o | aS 1 | e S 1 -> b | bS 1 | e but I think this area is wrong, because there is a chance that the number of b 's can be greater than a 's. 回答1: How to write CFG with example a m b n L = {a m b n | m >= n}. Language description: a m b n consist of a followed by b where number of a are equal or more then number of b . some example strings: {^

How to simplify JavaScript/ECMAScript array literal production?

元气小坏坏 提交于 2019-12-13 17:44:27
问题 I currently implementing a JavaScript/ECMAScript 5.1 parser with JavaCC and have problems with the ArrayLiteral production. ArrayLiteral : [ Elision_opt ] [ ElementList ] [ ElementList , Elision_opt ] ElementList : Elision_opt AssignmentExpression ElementList , Elision_opt AssignmentExpression Elision : , Elision , I have three questions, I'll ask them one by one. I have tried to simplify/to rewrite the ArrayLiteral production depicted above and finally arrived to the following production

Finding a language that is not LL(1)?

亡梦爱人 提交于 2019-12-13 13:05:59
问题 I've been playing around with a lot of grammars that are not LL(1) recently, and many of them can be transformed into grammars that are LL(1). However, I have never seen an example of an unambiguous language that is not LL(1). In other words, a language for which any unambiguous grammar for the language is not LL(1)), nor do I have any idea how I would go about proving that I had found one if I accidentally stumbled across one. Does anyone know how to prove that a particular unambiguous

How do I match a hex array in perl6 grammar

大城市里の小女人 提交于 2019-12-13 12:55:44
问题 I have a string like "39 3A 3B 9:;" and i want to extract "39, 3A, 3B" I have tried my $a = "39 3A 3B 9:;"; grammar Hex { token TOP { <hex_array>+ .* } token hex_array { <[0..9 A..F]> " " } }; Hex.parse($a); But this doesn't seem to work. And even this doesn't seem to work. my $a = "39 3A 3B "; grammar Hex { token TOP { <hex_array>+ } token hex_array { <[0..9 A..F]> " " } }; Hex.parse($a); I did try Grammar::Tracer both TOP and hex_array fail TOP | hex_array | * FAIL * FAIL 回答1: <[abcdef...]>

Parsing boolean expression without left hand recursion

烈酒焚心 提交于 2019-12-13 11:43:07
问题 I'm trying to match this f(some_thing) == 'something else' f(some_thing) is a function call, which is an expression == is a boolean operator 'something else' is a string, which also is an expression so the boolean expression should be expression operator expression The problem is I can't figure out how to do that without left recursion These are my rules expression = bool_expression / function_call / string / real_number / integer / identifier bool_expression = l:expression space* op:bool

Operator Associativity

自古美人都是妖i 提交于 2019-12-13 07:04:54
问题 I have the following EBNF expression grammar: <expr> -> <term> { (+|-) <term> } <term> -> <factor> { (*|/|%) <factor> } <factor> -> <pow> { ** <pow> } <pow> -> ( <expr> ) | <id> <id> -> A | B | C I need to determine if the grammar enforces any particular associativity for its operators, or if that would have to be implemented in the parser code. From what I have read so far, it doesn't look like it does, but I am having a hard time understanding what causes associativity. Any help would be

How can I force Bison to shift to resolve a conflict?

冷暖自知 提交于 2019-12-13 04:45:52
问题 I'm building this grammar for a simple programming language (already solved previous ambiguity issues: Can't figure out why Bison is throwing "Rules useless in parser due to conflicts"). This is my complete grammar: http://pastebin.com/yBHLSP0z And this is the output file from Bison: http://pastebin.com/eAma3gWy (sorry, they're in Spanish, but I think they're pretty self-explanatory) The thing is, I'm still getting one shift/reduce error at state 107 (I'm translating it): state 107 31 factor:

Removing Left Recursion Algorithm by Hand

别说谁变了你拦得住时间么 提交于 2019-12-13 04:45:25
问题 I'm unsure how to finish the left recursion removal algorithm for this grammar. S ::= a B | B S b | S a | S B A | b B ::= S b A | B B | A S B | a D ::= b a | S b A ::= b S A | b | a b Here is my working. using the order S, B, D, A. S ::= a B M | B S b M | b M M ::= a M | B A M | ε B ::= a B M b A | B S b M b A | b M b A | B B | A S B | a B ::= a B M b A N | b M b A N | A S B b A N | a N N ::= S b M N | B N | ε How should I progress from here? 回答1: From the Dragon Book. Given the following

How would I declare a grammar rule for a number of tags in any order?

家住魔仙堡 提交于 2019-12-13 04:25:10
问题 I am trying to write a compiler for a formating language.This language has a start and an end property and a set of document and text properties. The first is just info for the document itself where as the second is the actual document (titles, paragraphs, lists... the usual). The first set must always follow the start property and must contain all properties BUT in any order the user might like. Assuming that my tokens for the properites are PROP1, PROP2, PROP3 and PROP4 I can use recursion

Issue with a Jison Grammar, Strange error from generate dparser

左心房为你撑大大i 提交于 2019-12-13 02:56:48
问题 I am writing a simple Jison grammar in order to get some experience before starting a more complex project. I tried a simple grammar which is a comma separated list of numeric ranges, with ranges where the beginning and ending values were the same to use a single number shorthand. However, when running the generated parser on some test input I get an error which doe snot make alot of sense to me. Here is the grammar i came up with: /* description: Parses end executes mathematical expressions.