Anybody into lex and yacc ?

This is a discussion on Anybody into lex and yacc ? within the Tech Board forums, part of the Community Boards category; I need to learn lex and yacc (i'm using bison and flex for them) can anybody suggest any good tutorial ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    Anybody into lex and yacc ?

    I need to learn lex and yacc (i'm using bison and flex for them) can anybody suggest any good tutorial for them because I only found junk by searching on google. Tutorials that don't seem to be very helpfull.

    I'm sorry if I'm not asking in the right place, this being a C forum. I thought that because they are so C related languages somebody here knows how to help me.

    Thank you very much!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    Moved to Tech.

    Or a book perhaps - try a library
    http://www.amazon.com/lex-yacc-Doug-...4750571&sr=8-1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    I read somewhere in this form that there is an multi threaded alternative.
    I think this is a serious drawback did you consider the alternatives.
    maybe they are easier to master as well.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by spank View Post
    I need to learn lex and yacc (i'm using bison and flex for them) can anybody suggest any good tutorial for them because I only found junk by searching on google. Tutorials that don't seem to be very helpfull.

    I'm sorry if I'm not asking in the right place, this being a C forum. I thought that because they are so C related languages somebody here knows how to help me.

    Thank you very much!
    I don't know of any good tutorials, but if you have specific questions I can try to answer them.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    Code:
    %{
    void yyerror(char*);
    %}
    %token NUMAR VARIABILA
    
    %%
    
    operand:	NUMAR
    			| operand '+' NUMAR
    		;
    
    adunare:	operand '+' operand {
    				printf("%d\n",$1+$3);
    			}
    			;
    			
    
    %%
    
    int main()
    {
    	yyparse();
    	
    	return 0;
    }
    
    void yyerror(char *s)
    {
    	printf("Eroare: %s\n",s);
    }
    why is this grammar wrong ?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    A multi-threaded alternative to a parser? I don't really think so. At most, you could build a producer/consumer model with the lexer and parser, but since parsing is largely I/O-bound, the gains would be rather irrelevant.

    The grammar is wrong because it is ambiguous.

    The expression "1 + 2" could be
    NUMAR=operand '+' NUMAR
    or
    NUMAR=operand=adunare '+' NUMAR=operand=adunare.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    why is that ambiguous? please explain in more words because I don't understand.

    Code:
    instructiune      :  expresie { printf("%d\n",$1); }
    ;
    
    expresie          :       expresie '+' NUMAR { $$=$1+$3;}
    |       expresie '-' NUMAR { $$=$1-$3;}
    |       NUMAR              { $$=$1;   }
    ;
    this is better i belive... but why does it work only for the first sequence i enter ? and for the second it gives me syntax error
    Last edited by spank; 07-19-2007 at 03:06 AM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    How can I make it more obvious? There are two ways to resolve the expression with your previous grammar, and neither is preferable to the other, so, since yacc can't decide which to use, it fails.

    The new grammar is better. But I don't understand your error.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    Code:
    instructiune      :  expresie { printf("%d\n",$1); } instructiune
    |;
    
    expresie          :       expresie '+' NUMAR { $$=$1+$3;}
    |       expresie '-' NUMAR { $$=$1-$3;}
    |       NUMAR              { $$=$1;   }
    ;
    I should of done this... this is because i wanted to enter expresions one after the other. thank you... more questions coming soon :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Three Address Code generation using Lex, Yacc
    By Raman in forum C Programming
    Replies: 1
    Last Post: 11-04-2008, 08:04 AM
  2. using YACC and lex to make your own shell...
    By YankeePride13 in forum Linux Programming
    Replies: 2
    Last Post: 12-28-2005, 09:00 AM
  3. Yacc and Lex
    By Chronom1 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2005, 07:03 AM
  4. Lex and Yacc
    By Mystic_Skies in forum Linux Programming
    Replies: 2
    Last Post: 12-05-2004, 03:06 PM
  5. Code for C Compiler using lex and yacc
    By jaadugary in forum C Programming
    Replies: 0
    Last Post: 05-10-2003, 09:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21