• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Help on error handling in a complier

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
DancingFighterG
Asked:
DancingFighterG
  • 2
1 Solution
 
stefan73Commented:
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
 
DancingFighterGAuthor Commented:
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
 
stefan73Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now