Solved

Help on error handling in a complier

Posted on 2004-03-31
5
199 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
[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
5 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10722253
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
ID: 10725721
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
ID: 10732869
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

Industry Leaders: 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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

762 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