3 Replies - 437 Views - Last Post: 08 September 2013 - 09:13 PM Rate Topic: -----

#1 pcboyyyy  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 29-August 10

*lex-yacc*

Posted 07 September 2013 - 10:42 PM

I am trying to grasp it for work to build a compiler I need. The last time I even remember doing anything with this was when I was an undergrad.

I understand a good part of it, but I am not sure how to actually run the program I am trying to create.

For now I am just trying to do something simple then move forward. Here's some details of what I'm trying to do.

1.Write a flex program such that it generates a scanner tokens defined below:
1) Identifiers: An identifier is a sequence of letters, digits, and underscores, starting with a letter. Uppercase letters are distinguished from lowercase.
2) Integer literals: A sequence of decimal digits is an integer constant that denotes the corresponding integer value. In this appendix the symbol INTEGER_LITERAL stands for an integer constant
3) operators which are listed below (quoted):
"&&" | "||" | "==" | "!=" | "<" | ">" | "<=" | ">=" | "+" | "-" | "*" | "/"

I pulled these instructions from a university web site.

Can someone please enlighten me? :whistling:

This post has been edited by pcboyyyy: 07 September 2013 - 10:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: *lex-yacc*

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2114
  • View blog
  • Posts: 3,240
  • Joined: 21-June 11

Re: *lex-yacc*

Posted 08 September 2013 - 09:35 AM

View Postpcboyyyy, on 08 September 2013 - 07:42 AM, said:

I am not sure how to actually run the program I am trying to create.


1. Write the lex file.
2. Use flex to compile the lex file to .c code (flex lexer.l).
3. Use your C compiler to compile the .c file to an executable (gcc ley.yy.c -o mylexer).
4. Run the executable (./mylexer).

This post has been edited by sepp2k: 08 September 2013 - 09:40 AM

Was This Post Helpful? 1
  • +
  • -

#3 pcboyyyy  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 29-August 10

Re: *lex-yacc*

Posted 08 September 2013 - 10:52 AM

View Postsepp2k, on 08 September 2013 - 09:35 AM, said:

View Postpcboyyyy, on 08 September 2013 - 07:42 AM, said:

I am not sure how to actually run the program I am trying to create.


1. Write the lex file.
2. Use flex to compile the lex file to .c code (flex lexer.l).
3. Use your C compiler to compile the .c file to an executable (gcc ley.yy.c -o mylexer).
4. Run the executable (./mylexer).


Very nice!

How can I run only the lex file to test it without linking to a C file? Or is that possible?
Was This Post Helpful? 0
  • +
  • -

#4 pcboyyyy  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 29-August 10

Re: *lex-yacc*

Posted 08 September 2013 - 09:13 PM

View Postsepp2k, on 08 September 2013 - 09:35 AM, said:

View Postpcboyyyy, on 08 September 2013 - 07:42 AM, said:

I am not sure how to actually run the program I am trying to create.


1. Write the lex file.
2. Use flex to compile the lex file to .c code (flex lexer.l).
3. Use your C compiler to compile the .c file to an executable (gcc ley.yy.c -o mylexer).
4. Run the executable (./mylexer).


What am I getting a premature EOF????


%{
#include <stdio.h>
%}
     
         DIGIT    [0-9]
         ID       [a-z][a-z0-9]*
     
         %%
     
         {DIGIT}+    {
                     printf( "An integer: %s (%d)\n", yytext,
                             atoi( yytext ) );
                     }
     
         if|then|begin|end|procedure|function        {
                     printf( "A keyword: %s\n", yytext );
                     }
     
         {ID}        printf( "An identifier: %s\n", yytext );
     
         "&&" | "||" | "==" | "!=" | "<" | ">" | "<=" | ">=" | "+" | "-" | "*" | "/"   printf( "An operator: %s\n", yytext );
     
         "{"[\^{}}\n]*"}"     /* one-line comments */
     
         [ \t\n]+          /* whitespace */
     
         .           printf( "Unrecognized character: %s\n", yytext );
     
         %%
     
         int main( int argc, char **argv )
             {
             ++argv, --argc;  /* skip over program name */
             if ( argc > 0 )
                     yyin = fopen( argv[0], "r" );
             else
                     yyin = stdin;
     
             yylex();
	     }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1