tags:

views:

92

answers:

0

Hi. I'm running a lexical analyzer using lex, and I've got it mostly correct, but my terminal gives strange output once I take out an ECHO statement I was using to help debug the code. With that statement, my output looks like this:

max@Max-Ubuntu:~/Desktop/Compiler Project/project2$ ./a.out <../cmmFiles/expression.cmm


VOIDID(){
INTID,ID,ID;
BOOLID,ID,ID;
ID(ID);
ID(ID);
ID(ID);
ID(ID);
ID=-ID-NUM+ID/NUM*(-NUM+ID*IDNUM);
ID(ID);
ID=ID>ID||ID>=ID;
IF(ID)ID(NUM);ELSEID(NUM);
ID=ID<ID&&ID<=ID;
IF(ID==TRUE)ID(NUM);ELSEID(NUM);
ID=ID&&!ID||!ID&&ID;
IF(ID!=FALSE)ID(NUM);ELSEID(NUM);
}

While hard to read, that output is correct. Once I take out the ECHO statement, I instead get this:

max@Max-Ubuntu:~/Desktop/Compiler Project/project2$ ./a.out <../cmmFiles/expression.cmm
}F(ID!=FALSE)ID(NUM);ELSEID(NUM);;

It looks like it's only outputting the final line, except with an extraneous } near the beginning, what looks like half an IF token immediately after, and an extraneous ; at the end.

Is this some quirk of my terminal, or does removing that ECHO cause my lexer to screw up that badly? I'm hesitant to keep working until I know for sure what's going on here. Thanks for any answers.

Here's my lexer:

%{
    /*
    definitions of manifest constants
    -reserved words-
    BOOL, ELSE, IF, TRUE, WHILE, DO, FALSE, INT, VOID

    -Punctuation and operators-
    LPAREN, RPAREN, LBRACK, RBRACK, LBRACE, RBRACE, SEMI, COMMA, PLUS, MINUS, TIMES,
    DIV, MOD, AND, OR, NOT, IS, ADDR, EQ, NE, LT, GT, LE, GE

    -Other tokens-
    NUMBER, ID, PUNCT, OP
    */
#include <stdio.h>
#include <stdlib.h>
//#include "y.tab.h"

//int line = 1, numAttr;
//char *strAttr;
%}

/* regular definitions */

delim   [ \t]
ws      {delim}+
start   "/*"
one     [^*]
two     "*"
three   [^*/]
end     "/"
comment {start}({one}*{two}+{three})*{one}*{two}+{end}
letter  [A-Za-z]
digit   [0-9]
id      ({letter}|_)({letter}|{digit}|_)*
number  {digit}+

%%

{ws}        { /*no action and no return */}
{comment}   { /*no action and no return */}
[\n]        {ECHO; /*no action */} // <-- this is the ECHO in question.
bool        { printf("BOOL");}
else        { printf("ELSE");}
if          { printf("IF");}
true        { printf("TRUE");}
while       { printf("WHILE");}
do          { printf("DO");}
false       { printf("FALSE");}
int         { printf("INT");}
void        { printf("VOID");}



{id}        { printf("ID");}

{number}    { printf("NUM");}

"("         { printf("(");}
")"         { printf(")");}
"["         { printf("[");}
"]"         { printf("]");}
"{"         { printf("{");}
"}"         { printf("}");}
";"         { printf(";");}
","         { printf(",");}
"+"         { printf("+");}
"-"         { printf("-");}
"*"         { printf("*");}
"/"         { printf("/");}
"%"         { printf("%");}
"&"         { printf("&");}
"&&"        { printf("&&");}
"||"        { printf("||");}
"!"         { printf("!");}
"!="        { printf("!=");}
"="         { printf("=");}
"=="        { printf("==");}
"<"         { printf("<");}
"<="        { printf("<=");}
">"         { printf(">");}
">="        { printf(">=");}

%%

int main()
{
    yylex();
    printf("\n");
}

int yywrap(void)
{
    return 1;
}

here's the file it's analyzing:

/*   this program
 *   illustrates evaluation of 
 *   arithmetic and boolean
 *   expressions
 */

void main( ) {
    int m,n,p;
    bool a,b,c;
    scan(m);
    print(m);
    scan(n);
    print(n);
    p=-m-3+n/2*(-5+m*n%4);
    print(p);
    a=m>n || n>=p;
    if (a) print(1); else print(0);
    b=m<n && n<=p;
    if (b==true) print(1); else print(0);
    c=a && !b || !a && b;
    if (c!=false) print(1); else print(0);
}