Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7504315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:32:03+00:00 2026-05-29T21:32:03+00:00

I am reading in a file and for some reason i get a syntax

  • 0

I am reading in a file and for some reason i get a syntax error when i try an expression
like 5+5 however, if i do this 5 + 5 it works good. I am baffled why it would do this?

Here is my lex file( i will leave out main that reads in a file):

%{

 #include "y.tab.h"
 #include "stdio.h"
 #include <stdlib.h>

%}
%%
(\/\*([^*]|(\*+([^*/]|[\r\n])))*\*+\/)+ {}
\/\/[^\n]*\n          {}
fd                    { return FD; }
[\r\t\n]+             {}
[ ]*                  {}
bk                    { return BK;}
setc                  {return SETC;}
[-+]?[0-9]+           { yylval = atoi(yytext); return NUMBER;}
fd[0-9]+              { }
rt                    {return RT;}
pink                  {return COLOR_TYPE;}
magenta               {return COLOR_TYPE; }
if                    {return IF; }
ifelse                {return IFELSE; }
\[                    {return LBRACKET; }
\]                    {return RBRACKET; }
\<                    {return LESS_THAN; }
\>                    {return GREATER_THAN; }
\+                    {return PLUS; }
\-                    {return MINUS; }
\/                    {return DIVIDE; }
\*                    {return MULT; }
\(                    {return LPAREN;}
\)                    {return RPAREN;}
\=                    {return EQ;}

%%

Here is part of my yacc file that deals with the expression:

expr    : NUMBER     { printf("EXPR-->NUMBER: %d\n", $1);}
   |expr PLUS expr   {$$ = $1 + $3; printf("EXPR-->expression PLUS expression: %d\n", $$);}
   |expr DIVIDE expr {$$ = $1 / $3; printf("EXPR-->expression DIVIDE expression %d\n", $$);}
   |expr MULT expr   {$$ = $1 * $3; printf("EXPR-->expression MULTIPLY expression %d\n", $$);}
   |expr MINUS expr  {$$ = $1 - $3; printf("EXPR-->expression MINUS expression %d\n", $$);}
   |COLOR_TYPE       {printf("EXPR-->COLOR\n");}
   ;

would the problem be in the lex file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:32:04+00:00Added an answer on May 29, 2026 at 9:32 pm

    The tokenizer (lexer) returns these two to the parser: 5 and +5. Which by your grammar (and logically) is invalid.

    I think, you’d be better off, to alter your lexer and move the rules for operators up. (That means at least above the rule returning NUMBER).

    EDIT: After some thinking (EDIT #2: and a more than useful comment by Jerry Coffin), I suggest changing the lexical rule for NUMBER to [0-9]+. In order for the parser to still accept input like “+123” or “-123”, you should add this to your grammar:

    %left PLUS MINUS ...
    %right UNARY
    
    %%
    
    expr   : number
           | expr PLUS expr
          ...
           ;
    
    number : PLUS NUMBER %prec UNARY {$$ = $2}
           | MINUS NUMBER %prec UNARY {$$ = -$2}
           | NUMBER 
           ;
    

    This will allow for an unary + or - before any number while still giving the operators + and - a higher precedence.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a text file with this format: grrr,some text,45.4321,54.22134 I just have
For some reason I've having problems reading this session variable within an ajax document,
I'm having some trouble reading data from a file into a vector of Orders.
I've always used a *.h file for my class definitions, but after reading some
I am reading a file byte-by-byte. Say for example i have this byte: 0x41
Are there any good configuration file reading libraries for C\C++ that can be used
Ok, I am reading in dat files into a byte array. For some reason,
What I'm doing: I am reading some data off a file several times while
This could be a rather crazy question, but I wanted to get some insight
i just create reading file feature. In general, what kind PHP class that usually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.