?
Solved

extending if then else statments in lexical analysis

Posted on 2003-03-22
2
Medium Priority
?
239 Views
Last Modified: 2010-04-15
How would I extend the syntax of the if then else end statement and the if then end statement in the syn.y file, to allow optionally repeating "elif" clauses.
An example: if a>b then c:=10
            elif a<b then c:=100
            else c:=0 end;

Here are the two files for your reference:
------------------------------------------------------------------------------------
Grammar syn.y file

%{
/*            pl 1/2

    this is compiler for a very little language called pl1/2
    (pronounced pl-a-half).
    this is a syntax checker version
*/

#include <stdio.h>
extern int fatalerror;  
%}

%start prog
%token IDENT NUMBER
%token PROGSY BEGINSY IFSY WHILESY THENSY ELSESY DOSY
%token ENDSY ASSIGNSY CONSTSY VARSY WRITESY READSY
%nonassoc '<' '>' '=' '#'
%left '+' '-'
%left '*'

%%
/*  beginning of grammar rules
    ==========================
*/

prog:
      PROGSY ';' decls BEGINSY stats ENDSY '.'
          { /* found a program, */
                if (fatalerror > 0 )
                printf("errors detected\n");
            else
                      printf("program found\n");
        }
   ;

decls:
      /* empty */ { printf("doing: decls\n"); }
   |
decls decl { printf("doing: decls decl\n"); }
   ;

stats:
      stat { printf("doing: stat\n"); }
   |
      stats ';' stat  { printf("doing: stats ; stat\n"); }
   ;

decl:
      CONSTSY IDENT '=' NUMBER ';' { printf("doing: CONST\n"); }
   |
      VARSY IDENT ':' type ';' { printf("doing: VAR\n"); }
   |
      error ';' { fatal("error in declarations",""); }
   ;

stat:
      /* empty */
   |
      IDENT ASSIGNSY expr   { printf("doing: ASSIGN\n"); }
   |
      WHILESY expr DOSY stats ENDSY  { printf("doing: WHILE\n"); }
   |
      IFSY expr THENSY stats ENDSY { printf("doing: IF THEN\n"); }
   |
      IFSY expr THENSY stats ELSESY stats ENDSY { printf("doing: IF THEN ELSE\n"); }
   |
      READSY '(' IDENT ')'  { printf("doing: READ\n"); }
   |
      WRITESY '(' expr ')'    { printf("doing: WRITE\n"); }
   |
      error { fatal("error in statement",""); }
   ;
   
expr:
      expr '+' expr   { printf("doing: + \n"); }
   |
      expr '*' expr   { printf("doing: * \n"); }
   |
      expr '-' expr     { printf("doing: - \n"); }
   |
      '(' expr ')'   { printf("doing: (e) \n"); }
   |
      IDENT        { printf("doing: IDENT \n"); }
   |
      NUMBER       { printf("doing: NUMBER \n"); }
   |
      expr '<' expr        { printf("doing: < \n"); }
   |
      expr '>' expr        { printf("doing: > \n"); }
   |
      expr '=' expr          { printf("doing: = \n"); }
   |
      expr '#' expr      { printf("doing: # \n"); }
   |
      error
         { fatal("error in expression",""); }
   ;

type:
      IDENT      { printf("doing: TYPE \n"); }
   ;
   
%%

int fatalerror=0;

int fatal(char *s){
    fatalerror++;
      fprintf(stderr,"%s\n",s);
}
int yyerror(char *s){
      fprintf(stderr,"%s\n",s);
}

int main() {
    printf("starting...\n");
    yyparse();
}
--------------------------------------------------------------------------
Lexical analyser lex.l file

%{
#include "syn.tab.h"
%}

DIGIT    [0-9]
ID       [a-zA-Z][a-zA-Z0-9]*

%%
[ \t\n]+    /* eat up whitespace */


{DIGIT}+    {  yylval = atoi(yytext);
               return NUMBER;
            }
begin       { return BEGINSY; }
do          { return DOSY; }
end         { return ENDSY; }
program     { return PROGSY; }
if          { return IFSY; }
while       { return WHILESY; }
then        { return THENSY; }
else        { return ELSESY; }
var         { return VARSY; }
const       { return CONSTSY; }
write       { return WRITESY; }
read        { return READSY; }


{ID}        { return IDENT; }
":="        { return ASSIGNSY; }

"+"|"-"|"*"|"/"   { return yytext[0]; }
"."|"("|")"|";"   { return yytext[0]; }
"="|"#"|"<"|">"   { return yytext[0]; }
":"               { return yytext[0]; }

.           printf( "Unrecognized character: %s\n", yytext );
%%



0
Comment
Question by:rschippendale
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 5

Expert Comment

by:ecw
ID: 8188362
ifexp: IFSY expr THENSY stats ENDSY

     ;

optionalelse: /* empty */
     | ELSIFSY stat
0
 
LVL 5

Accepted Solution

by:
ecw earned 300 total points
ID: 8188363
ifexp: IFSY expr THENSY stats ENDSY

     ;

optionalelse: /* empty */
     | ELSIFSY stat
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question