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 7673305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:28:55+00:00 2026-05-31T16:28:55+00:00

hi there i’m trying to make a simple parser and using lex and yacc.

  • 0

hi there i’m trying to make a simple parser and using lex and yacc. the thing is i wanna print my own error messages rather than error symbol used by yacc which prints syntax error. for example this is my yacc code;

%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
extern FILE *yyin;
extern int linenum;
%}

%token INTRSW IDENTIFIER INTEGER ASSIGNOP SEMICOLON DOUBLEVAL DOUBLERSW COMMA 
%token IF ELSE WHILE FOR
%token CLOSE_BRA OPEN_BRA CLOSE_PARA OPEN_PARA EQ LE GE
%token SUM MINUS MULTIP DIV

%left OPEN_BRA OPEN_PARA
%left MULTIP DIV
%left SUM MINUS

%union 
{
        int number;
        char* string;
}

%token <number> INTEGER
%token <string> IDENTIFIER

%%
program: 
    statement_list
    ;

statement_list:
        statement_list statement
        |
        statement
        ;

statement:
    if_statement OPEN_BRA statement_list CLOSE_BRA
    |
    if_statement 
    |
    assignment_block
    |
    single_assignment 
    ;

if_statement:
    IF OPEN_PARA condition_statement CLOSE_PARA
    ;

condition_statement:
    logical_expression
    ;

logical_expression:
    expression EQ expression
    |
    expression LE expression
    |
    expression GE expression
    ;

expression:
    double
    |
    IDENTIFIER
    |
    OPEN_PARA expression CLOSE_PARA
    |
    expression MULTIP expression
    |
    expression DIV expression
    |
    expression SUM expression
    |
    expression MINUS expression
    ;

assignment_block:
    integer_assignment_block
    | 
    double_assignment_block
    ;

integer_assignment_block:
    INTRSW integer_assignment_list SEMICOLON
    ;

double_assignment_block:
    DOUBLERSW double_assignment_list SEMICOLON
    ;

integer_assignment_list:
    integer_assignment
    |
    integer_assignment_list COMMA integer_assignment
    ;

double_assignment_list:
    double_assignment
    |
    double_assignment_list COMMA double_assignment
    ;

single_assignment:
    IDENTIFIER ASSIGNOP double SEMICOLON
    |
    IDENTIFIER ASSIGNOP IDENTIFIER SEMICOLON
    |
    error ';' { printf("You made en error"); }  
    ;

integer_assignment:
    IDENTIFIER ASSIGNOP INTEGER
    |
    IDENTIFIER
    ;

double_assignment:
    IDENTIFIER ASSIGNOP double
    |
    IDENTIFIER
    ;

double:
    DOUBLEVAL
    |
    INTEGER 
    ;

%%
void yyerror(char *s){
    fprintf(stderr,"%s Error at line: %d\n",s, linenum);
}
int yywrap(){
    return 1;
}
int main(int argc, char *argv[])
{
    /* Call the lexer, then quit. */
    yyin=fopen(argv[1],"r");
    yyparse();
    fclose(yyin);
    return 0;
}

so this the block i add error statement.

single_assignment:
    IDENTIFIER ASSIGNOP double SEMICOLON
    |
    IDENTIFIER ASSIGNOP IDENTIFIER SEMICOLON
    |
    error ';' { printf("You made en error"); }  
    ;

so i write a=7 (without ‘;’) in a line it just says syntax error Error at line:7. so where is my error message_? i will be very very glad if you can help me about this issue. and thanks anyway

  • 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-31T16:28:57+00:00Added an answer on May 31, 2026 at 4:28 pm

    so i write a=7 (without ‘;’) in a line it just says syntax error Error
    at line:7. so where is my error message_?

    Your error action tells Yacc to discard tokens until it finds a ‘;’ token. Until that happens, it cannot reduce by that rule.

    Also, you should execute yyerrok; somewhere in the rule body to signal to the parser that recovery has been achieved. Optionally, you can use yyclearin; to also throw away the token that triggered the error. Doing this is tricky because you’re guessing that the token is inappropriate. It can actually be the right token, but something else before it is missing! E.g you’re seeing a semicolon, because a closing brace was left out, etc.

    Error actions will not replace the behavior of calling yyerror. When a syntax error occurs, the parser will call yyerror with a message (usually “syntax error”), and then are the error productions considered. Error productions are not anything like “custom overrides for syntax errors”. (It looks like you’re expecting “syntax error” to be replaced with your own generic error message “You made an error”.)

    In the error production, if you can guess the nature of the error, you can print an additional diagnostic that is more helpful.

    One thing that is useful is the yychar variable which tells you the lookahead token. You can check this in the error recovery rule and try to guess what went wrong based on its value. You can check this not only for your own token types but for the value YYEOF which indicates that the syntax error is due to reaching not some bad token, but the end of input.

    I wrote a parser in which, in some error productions, I simply took the token from yychar and converted it to its descriptive name and printed the message “unexpected in “. This is better than nothing; it tells the user at which token does the syntax depart from what is expected.

    Also, yacc parsers can produce diagnostics even in a correct parse! (Obviously; for instance, how else would you implement warnings for a language.) Basically you need some central error reporting function that you can call yourself, and which yyerror will also call. That function should set a flag indicating whether or not fatal errors occurred (or keep a count of how many errors, warnings, etc). You may want to, for instance, throw away the parse tree and bail the program with a failed termination status, if there were fatal errors, even if the parser recovered from any syntax errors.

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

Sidebar

Related Questions

There are several types of objects in a system, and each has it's own
There must be a simple solution to this, but after 4 hours of browsing
There is a known issue with using jquery fadeOut, fadeIn, and fadeToggle, when fading
There is a constant I'm using from the iOS SDK. I printed out the
There have been several questions on SO regarding getting Contacts numbers using the Contacts
There are few apps like Strava which records users movements using GPS. It also
There is a conversion process that is needed when migrating Visual Studio 2005 web
There are two weird operators in C#: the true operator the false operator If
There are two popular closure styles in javascript. The first I call anonymous constructor
There is previous little on the google on this subject other than people asking

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.