grammar

Why are these conflicts appearing in the following yacc grammar for XML

江枫思渺然 提交于 2019-12-22 13:07:14
问题 I have the following XML grammar which works fine: program : '<' '?'ID attribute_list '?''>' root ; root : '<' ID attribute_list '>' node_list '<''/'ID'>' ; node_list : node_s | node_list node_s ; node_s : node | u_node | ID ; node : '<' ID attribute_list '/''>' ; u_node :'<' ID attribute_list '>' node_list '<''/'ID'>' |'<' ID attribute_list '>' '<''/'ID'>' ; attribute_list : attributes | ; attributes : attribute | attributes attribute ; attribute : ID ASSIGNOP '"' ID '"' | ID ASSIGNOP '"'

Do I Have an LL(1) Grammar for This Subset of XML?

余生颓废 提交于 2019-12-22 12:21:07
问题 I'm about to make a parser for a fictional subset of XML with the following EBNF grammar: DOCUMENT ::= ELEMENT ELEMENT ::= START_TAG (ELEMENT | DATA)* END_TAG | EMPTY_TAG START_TAG ::= < NAME ATTRIBUTE* > END_TAG ::= </ NAME > EMPTY_TAG ::= < NAME ATTRIBUTE* /> ATTRIBUTE ::= NAME = STRING The above is the grammar 'as is', without any changes. Here is my attempt at converting it to LL(1): DOCUMENT ::= ELEMENT EOF ELEMENT ::= PREFIX > ELEMENT_OR_DATA END_TAG | PREFIX /> PREFIX ::= < NAME OPT

Do I Have an LL(1) Grammar for This Subset of XML?

徘徊边缘 提交于 2019-12-22 12:21:04
问题 I'm about to make a parser for a fictional subset of XML with the following EBNF grammar: DOCUMENT ::= ELEMENT ELEMENT ::= START_TAG (ELEMENT | DATA)* END_TAG | EMPTY_TAG START_TAG ::= < NAME ATTRIBUTE* > END_TAG ::= </ NAME > EMPTY_TAG ::= < NAME ATTRIBUTE* /> ATTRIBUTE ::= NAME = STRING The above is the grammar 'as is', without any changes. Here is my attempt at converting it to LL(1): DOCUMENT ::= ELEMENT EOF ELEMENT ::= PREFIX > ELEMENT_OR_DATA END_TAG | PREFIX /> PREFIX ::= < NAME OPT

How can I push the current execution state into a stack so that I can continue from it later?

只愿长相守 提交于 2019-12-22 09:31:11
问题 Imagine a simple grammar: (a|ab)c Which reads (a or ab) followed by c. The parse tree would look like this: and / \ or c / \ a ab Now given it this input: abc We would traverse first down the left side of the tree, and match "a", then go back up a level. Since "a" matched, the "or" is also true, so move on to the "c". "c" does not match, and we hit the end of the road. But there was an alternate path it could have taken; had we traversed down to "ab", we would have found a match. So what I

Parse Variables Using DCG

廉价感情. 提交于 2019-12-22 09:30:25
问题 I am having trouble parsing sequences that begin with capital letters into variables using Prolog's DCG notation. For instance, if I have the string f a X y Z X and a DCG that parses this string, is there any way to parse each capitalized letter into a unique Prolog variable. E.g., parse Y to a variable and each X to a variable? The intended application would be to build the functor T = f(a,X,y,Z,X) via a DCG rule ending with the statement {T =.. [Head|Args]} 回答1: Maybe you are looking for

Parse tree generation with Java CUP

大憨熊 提交于 2019-12-22 08:07:03
问题 I am using CUP with JFlex to validate expression syntax. I have the basic functionality working: I can tell if an expression is valid or not. Next step is to implement simple arithmetic operations, such as "add 1". For example, if my expression is "1 + a", the result should be "2 + a". I need access to parse tree to do that, because simply identifying a numeric term won't do it: the result of adding 1 to "(1 + a) * b" should be "(1 + a) * b + 1", not "(2 + a) * b". Does anyone have a CUP

init-declarator-list and the GNU GCC attribute grammar

ぐ巨炮叔叔 提交于 2019-12-22 06:45:09
问题 I am revamping an inhouse C language bison/flex-based parser, amongst others introducing proper __ attribute__ support. Since I cannot find any official BNF-style grammar which describes GNU GCC __ attribute__ idea (except the http://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html document) I am extracting bits and pieces from the C++x11 standard and comments in various implementations found over the web. I have almost got it done (at least when it comes to parsing examples included in GCC's

Making a Grammar LL(1)

坚强是说给别人听的谎言 提交于 2019-12-22 04:57:10
问题 I have the following grammar: S → a S b S | b S a S | ε Since I'm trying to write a small compiler for it, I'd like to make it LL(1). I see that there seems to be a FIRST/FOLLOW conflict here, and I know I have to use substitution to resolve it, but I'm not exactly sure how to go about it. Here is my proposed grammar, but I'm not sure if it's correct: S-> aSbT | epsilon T-> bFaF| epsilon F-> epsilon Can someone help out? 回答1: In his original paper on LR parsing, Knuth gives the following

Resolving reduce/reduce conflict in yacc/ocamlyacc

丶灬走出姿态 提交于 2019-12-22 03:52:40
问题 I'm trying to parse a grammar in ocamlyacc (pretty much the same as regular yacc) which supports function application with no operators (like in Ocaml or Haskell), and the normal assortment of binary and unary operators. I'm getting a reduce/reduce conflict with the '-' operator, which can be used both for subtraction and negation. Here is a sample of the grammar I'm using: %token <int> INT %token <string> ID %token MINUS %start expr %type <expr> expr %nonassoc INT ID %left MINUS %left APPLY

Resolving reduce/reduce conflict in yacc/ocamlyacc

谁说胖子不能爱 提交于 2019-12-22 03:52:10
问题 I'm trying to parse a grammar in ocamlyacc (pretty much the same as regular yacc) which supports function application with no operators (like in Ocaml or Haskell), and the normal assortment of binary and unary operators. I'm getting a reduce/reduce conflict with the '-' operator, which can be used both for subtraction and negation. Here is a sample of the grammar I'm using: %token <int> INT %token <string> ID %token MINUS %start expr %type <expr> expr %nonassoc INT ID %left MINUS %left APPLY