Solved

Help on error handling in a complier

Posted on 2004-03-31
5
194 Views
Last Modified: 2012-05-04
Hello, I'm in the process of building the syntax part of a compiler and I am having a problem handling an error wants I get it. The error that once I read a line and the statement does not have a semi-colon after it I'm trying to token flush of semi colon and move on to the next line to see if it has any errors. Now the problem is that right now it throws an error saying that a semi colon is not present but I am having a problem capturing that semi-colon in my token flush and move in to the next line. This is what I have right now for this:

catch(string e)
{
    parse_level = program_level;
    cerr << e << endl;
    cout << "--- begin token flush ---" << endl;
   // MAIN is 56, IDENT is 14, EOF is 40
   while (!(current_token_code == 56 || current_token_code == 14 ) && current_token_code != 40)
  {
// 101 = semi-colon      
if(current_token_code != 101)
      {
         cout << Lexeme << endl;
         current_token_code =
         cout << Lexeme << endl;
         current_token_code = lex();
   }
      cout << "---  end token flush  ---" << endl;

     // EOF is 40
     if (current_token_code == 40)
     {
       throw (string("End of file"));
     }
                  
}

Right now, my catch get's and error statement from my fucntion called decl() that finds that a semi colon is missing. Once that occurs I begin my token flush but it flushes the next token which is the next statement which is int array[]; What are some solution for fixing this if this is fixable!
0
Comment
Question by:DancingFighterG
  • 2
5 Comments
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
Hi DancingFighterG,
> current_token_code =
>          cout << Lexeme << endl;

I hope this is not your error :-)

If I understood you correctly, you want to implicitly insert a semicolon at EOL when the user didn't add one, just as languages like Ruby or Lua do.

You should wrap this in your yylex() function - see
http://dinosaur.compilertools.net/bison/bison_7.html

for further reference. Yacc or bison must catch it, but they're "pulling" a value via yylex(), instead of you "pushing" it. But a push behaviour can be easily implemented with a queue.

Cheers,
Stefan
0
 

Author Comment

by:DancingFighterG
Comment Utility
Hello Stefan, I don't want to insert semi-colon where the user didn't add one I just want to indicate that a semi-colon was not present when it needed to be there and then flush the value that is missing and move on to the next input line. For example, let's say the we have the following code

int bob;
int a.
bob = 8 + 6;

The int a =. should throw an error because it is expecting a semi rather then a . All I want is the token flush to flush the dot and then move to the next input statement bob = 8 + 6;
Now my only concern is what would I flush if the statment was like this:

int bob;
int a. , int b;
bob = 8 + 6;

The int b; is corrent but the int a. is not. Should I just flush that whole line and then move on to the next line?
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 50 total points
Comment Utility
DancingFighterG,
> int bob;
> int a.
> bob = 8 + 6;

If this follows C syntax, the lexer can't find any error here - int a.bob = ... are fine tokens from lexer perspective.

The flushing only offers very limited help - I'd skip it.

Stefan
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now