COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### how to write a grammar rule with yacc/bison

• Email
• Follow

```Hi,
How to write such a grammar rule that it looks like "Y --> X1; X2;
X3; ..; X100;" but X1, X2,..., X100 can appear once and only once and
having no limitation on the order?

Best Regards,
Justin Cui

```
 0
Reply justincui (3) 4/15/2006 8:35:55 AM

See related articles to this posting

```justincui@gmail.com writes:
>      How to write such a grammar rule that it looks like "Y --> X1; X2;
> X3; ..; X100;" but X1, X2,..., X100 can appear once and only once and
> having no limitation on the order?

If you want to enforce that at the syntax level,  you'll have to write
a rule for each combination.

I would just write:

Y          : X-sequence ;
X_sequence : | X ';' X_sequence ;
X          : X1 | X2 | X3 | ... | X100 ;

and check at the semantic level that each X occured only once:

Y : X-sequence { if(duplicate_X(\$1){error("Duplicate X");} return \$1; } ;

--
__Pascal Bourguignon__                     http://www.informatimago.com/
Un chat errant
se soulage
dans le jardin d'hiver
Shiki
```
 0

```Pascal Bourguignon wrote:
> justincui@gmail.com writes:
> >      How to write such a grammar rule that it looks like "Y --> X1; X2;
> > X3; ..; X100;" but X1, X2,..., X100 can appear once and only once and
> > having no limitation on the order?
>
> If you want to enforce that at the syntax level,  you'll have to write
> a rule for each combination.
>
> I would just write:
>
> Y          : X-sequence ;
> X_sequence : | X ';' X_sequence ;
> X          : X1 | X2 | X3 | ... | X100 ;
>
> and check at the semantic level that each X occured only once:
>
> Y : X-sequence { if(duplicate_X(\$1){error("Duplicate X");} return \$1; } ;

You can also check earlier,

%{
int seen[100];
%}

%%

Y : XSEQ '\n' ;
XSEQ : X | XSEQ ';' X ;
X : XTOK
{ printf("saw: %c\n",\$1);
if(seen[\$1-'0']) { yyerror("duplicate"); YYERROR; }
else seen[\$1-'0'] = 1;
};
XTOK : '1'|'2'|'3';

%%

int yylex(){
return yylval = getchar();
}

>
>
> --
> __Pascal Bourguignon__                     http://www.informatimago.com/
>         Un chat errant
> se soulage
>         dans le jardin d'hiver
>                                         Shiki

```
 0

2 Replies
494 Views

Similar Articles

12/11/2013 9:48:40 PM
page loaded in 26791 ms. (0)

Similar Artilces:

ECMAScript grammar for yacc/bison?
Hi, Does anybody know whether an ECMAScript grammar exists for yacc/bison ? Thanks very much in advance. Best Regards, Angel Todorov [When people have asked this questions before, they've found plenty of Javascript parsers but not one in yacc. -John] ...

Seeking bison/yacc grammar for Java language
Hi, Does anyone have a working yacc/bison grammar for the full java language? I've been tinkering away at one for a few days, but have hit a roadblock: When adding 'Type . class' to 'PrimaryNoNewArray' [1], I end up with 10 shift/reduce and 2 reduce/reduce conflicts, and most java sources fail to parse. But when I remove this, I only have 4 shift/reduce conflicts, and nearly all sources parse successfully (except those with 'Type . class' expressions. I don't want to battle any longer, in case someone has already solved the issues and is willing to share th...

How to write the preprocessor grammar for C++ using Yacc ?
Such as #include ,#define statement ect. Any help will be appreciated!And thanks in advance! [How about looking at cccp and other freely available C preprocessors? -John] ...

Bison Grammar
Does anyone know a good repository with bison grammar files, specially for markup languages? ...

Yacc = Bison???
Hi! Can someone tell me basic (if there is one) difference beetwen yacc and bison? I mean, are they the same only bison is a better version, do they use same input files? Thnx!!!! On 2006-03-29, Kamikazy <josip.hars@fer.hr> wrote: > Hi! Can someone tell me basic (if there is one) difference beetwen yacc and > bison? I mean, are they the same only bison is a better version, do they use > same input files? Bison is the GNU version of yacc (yacc predates GNU, and there are no doubt other yaccs). Bison does more than yacc, but should be upwardly compatible with files input ...

help_needed in bison/yacc
hi ever1, my input is verilog code, and i need to convert turnary expressions into if-else statements. please tell me if it is possible using Flex/Bison (i.e. pls tell me how to create interperter using flex/bison). All i wanted to say is, i don't want to take pain for writing grammer for whole verilog code. i just want to make changes in turnary expression. i.e. want a small script to change turnary expression into if-else statement. pls help. Thanks in Advance. -- Regards Vikas Talwar [If you don't want to parse the entire program, I'd suggest a kludge that uses regular expr...

writing ambiguous grammar...
Hi, I want to write a grammar for recognization both of IP Address and general text. But if the string contains any ambiguous string such as "68.", parser(lexer) may not be able to determine the string is IP Address or just a general text. which kind of grammar could distinguish the two different string? I'll be appreciated for your help. [Well, OK. Is this a string or an abbreviated IP address? 42. Unless you have some context that tells you what to expect, neither a computer parser nor a person can tell what it's supposed to be. -John] ...

lex / yacc grammar
does anyone knows where I can find a lex/yacc or flex/bison grammar for C++? [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] In article <1119630105.830598.65480@z14g2000cwz.googlegroups.com>, "Marcel" <marcel@kttf.net> wrote: > does anyone knows where I can find a lex/yacc or flex/bison grammar for > C++? Check the FAQ of the comp.compilers newsgroup, published there monthly, which perhaps has a reference. -- Hans Aberg [ See http://www.gotw.ca/resources/clcm.htm f...

prefix for yacc/bison
Hi, sorry, this question is offtopic, but didn't find any other newsgroup to ask. A question on yacc/bison: I've to work on two different machines. One of them is using yacc the other is using bison (by default as replacement for yacc). For a yacc file I've to change the default prefix yy. Thus, I've added the line %name-prefix="aa". This works fine for bison but issues an syntax error with yacc. Is there a way to change the prefix which work for both yacc and bison? Thank you Chris Christian Christmann <plfriko@yahoo.de> schrieb: > %name-prefix="a...

anything more convenient then bison/yacc?
Hi all, I am wondering if there are some newer reliable engines similar to yacc/bison, but with more syntactic sugar and wider class of the grammars. In particular some ideas/notions of regular expression would be fine. For example, something like: e? Matches element e in brackets zero or one times e* Matches element e in brackets zero or more times e+ Matches element e in brackets one or more times e{num} Matches element e in brackets num times e{min, max} Matches element e in brackets at least min times, but not more than max times where element e might be a compound elemnt...

ITE bison grammar problem
Hi all, I have right now a big grammar for a language I conveived to simplify another problem but that's not important right now. The fact is that I'm left with a reduce/reduce conflict which is rather serious and I can't solve it. I've managed to create a simple grammars that shows the conflict: %left '&' '+' %token id %start F %% F: "ite" '(' F ',' F ',' F ')' | E '&' E | M | id ; M: E "==" E; E: "ite" '(' E ',' E ',' E ')' | E '+' E...

YACC grammar for 8085 assembler.
Hi, I am looking for a YACC grammar for 8085 assembler. It does not have to be a perfect working grammar; any "starter" grammar which will get me on the right track will do just fine. I've search a bit, but couldn't find such a grammar. Help ? Cheers. Dale. Dale Lyons wrote: > I am looking for a YACC grammar for 8085 assembler. What exactly do you want to parse? A special assembly code format? > It does not have to be a perfect working grammar; any "starter" grammar > which will get me on the right track will do just fine. If you can't f...

what are the rules for writing header files?
Hello all, I have written a class with many private data members.and i am putting it in a separate dll file. Now when i link that file while writing my main program module,natuarally i have to use the header file of the class developed by me to access its functionality. so what should be there in the header file? Only the public methods declaration / data members of that class or the whole layout of the class along with its all private members? ( IMO the private section restricts the accessibility of the class and the user of the class is abstracted from the exact layout of the class.So ther...

Bison Parser negate production rules
Can any one help in explaining how to negate a production rule in Bison parser? e.g A:B B: C |D |E (here i want to write a block of statements or rules which will be executed if B doesnt match to C/D/E.) [Basically, you can't other than by enumerating all of the other possibilities. What do you really want to do here? -John] On 23 Mar 2007 22:16:22 -0400, royzlife@gmail.com wrote: >Can any one help in explaining how to negate a production rule in >Bison parser? >e.g A:B > B: C > |D > |E > (here i want to write a...

rules to generate a context free grammar
Hello. I am very new to this list, and I do not know if this is the good place to ask the following question. I know that every set that can be generated by a computer can also be generated by a context free grammar. My question is: looking at the program that generates a set, can we deduce the context free grammar that generates it? There are some rules for my problem? If not, there are clear rules to write the grammar for a set? Thanks in advance. Alin Soare. [It's trivial to write a CFG that recognizes any fixed set of strings, but that grammar isn't likely to be very useful. ...

Writing a rules parser in ActionScript/JavaScript
[Note to compiler types: Flex is Adobe's development framework, not the lexer generator. -John] We're trying to find the best approach to write/create a fairly simple rules parser for the Flex 2 environment. The idea is to create a sort of advanced validation engine that can process Drools-like notation, specifically the left hand side (conditional) part, eg: Cheese( type == "stilton" && price < 10, age == "mature" ) This would then be used for writing dynamic input validation. However, because the running environment will be the Flex (Flash) engine, w...

yacc grammar / expressions parsed wrong ?
Hi, I have a problem in getting my yacc grammer (byacc/j) / parser to handle arithmetic expressions correctly. - For the string "((b*c)+a)" the parser produces the equivalent expression (b*c)+a. - But for the string "a+(b*c)" the produced expression is (b*c)+(b*c). The parser code which creates the add-expression is { yyval.obj = Expression.add ( (Expression) val_peek(2).obj, (Expression) val_peek(0).obj ); } In the second case ("a+(b*c)") both val_peek(2).obj, val_peek(0).obj refer to the same Expression instance. Here are the relevant parts of my grammar:...

Parser testing while grammar rules are unknown
Hi. I'm sorry if this group is not relevant. I'll be thankful for any idea, on where to look for answer. Let's say, we have language somehow similar to ADA. And we have a parser /compiler for this language. And we know it contain bugs. We do not have grammar rules for language, instead, we have a tons of source code written in this language. We would like to test parser against possible bugs. We could take source code, alter it slightly and pass to parser and see, if it will return error. Or, we could apply Markov chains algorithm (which could alter human readable texts into humor...

looking for sample yacc/bison files
Hey. I'm looking for a set of lyacc/bison files of various sizes to test out a part of a program that I'm writing. I'm especially looking for smaller files. On Sep 9, 3:09 pm, Handy Gandy <handigandy...@yahoo.com> wrote: > Hey. I'm looking for a set of lyacc/bison files of various sizes to test > out a part of a program that I'm writing. I'm especially looking for > smaller files. I have some (look for lexer.l and parser.y): http://telegraphics.com.au/svn/exprparser/trunk/ http://telegraphics.com.au/svn/dpa/trunk/nova/ http://telegraphics.com.au/svn/dp...

shift/reduce conflicts in the YACC grammar of C99
The "old standard" ANSI/ISO C (C89) had a known shift-reduce conflict in its YACC grammar, because of the uncertainty of where to hang an else in nested if statements. This conflict was resolved to "shift" by YACC, which is fine for C's semantics. It appears that C99 introduced two new shift/reduce conflicts with these new derivations for postfix-expression: ( type-name ) { initializer-list } ( type-name ) { initializer-list , } This is called compound literals and allows things like: struct POINT p; p = (struct POINT) {x, y}; int *ap; ap = (int []...