Solved

Help on error handling in a complier

Posted on 2004-03-31
5
195 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
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Converting a float to a string function in c languange 10 204
Problem with form project 66 317
C Programming - If Statement 8 72
Super Scope, DHCP 5 53
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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

920 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

18 Experts available now in Live!

Get 1:1 Help Now