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
Solved

Help on error handling in a complier

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixing C++ and C code elegantly 10 158
Concatenate two strings Last and First Name 10 60
valid enum? 6 75
change colour of repeater control in asp.net c# 7 114
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

828 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