473,756 Members | 4,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

YACC/FLEX and reporting my errors

92 New Member
I am writing a parser to handle input commands and process them. There are some builtin in commands that it will recognize one of which is TOK_CMD with another keyword after it (all tokenized based on supported commands).

The processing is working great for real commands, so moving on to error detection. I have the rule:
Expand|Select|Wrap|Line Numbers
  1. | TOK_CMD error TOK_EOL
  2.   {
  3.   printf ("illegal command dummy\n");
  4.   }
  5. | error TOK_EOL
  6.   {
  7.   printf ("this is not a command\n");
  8.   }
at the end of my grammar. The idea is that I want to treat "unknown commands" differently then other unknown text. The trap seems to work perfectly, but to make it better at reporting I want to spit out the text of 'error' which could be any number of unmatched tokens or unmatched code. Note I do not want the entire line, just the unmatched part (ie missing the TOK_CMD part). Its been years since I used YACC so hoping this is easy and I just forgot.
Aug 17 '07 #1
5 2173
JosAH
11,448 Recognized Expert MVP
Can you use '$1' as a string (i.e. a char*)? As in:

Expand|Select|Wrap|Line Numbers
  1.   printf ("illegal command: %s\n", $1);
  2.  
kind regards,

Jos
Aug 17 '07 #2
Benny the Guard
92 New Member
Yes, the TOK_CMD can be used as a string. But the part i want is what 'error' represents not the value of TOK_CMD (although that may be important as well).

I have the whole line in a char* buffer. So in theory maybe I could do:

Expand|Select|Wrap|Line Numbers
  1. | TOK_CMD error TOK_EOL
  2. {
  3.  
  4. const char *begin_err_str = strstr (MyLineBuffer, $1);
  5. if (begin_err_str)
  6.      // advance to the end of TOK_CMD
  7.      begin_err_str += strlen ($1);
  8.  else
  9.      // Should never happen but just in case
  10.      begin_err_str = My_Line_Buffer;
  11.  
  12. printf ("%s is not a supported command dummy\n", begin_err_str);
  13. }
Of course ther newline is still in begin-err_string but I can remove that easily as well. A bit clunky but if you cannot use the $2 on error then its all I have to go on.
Aug 17 '07 #3
JosAH
11,448 Recognized Expert MVP
Yes, the TOK_CMD can be used as a string. But the part i want is what 'error' represents not the value of TOK_CMD (although that may be important as well).

I have the whole line in a char* buffer. So in theory maybe I could do:

Expand|Select|Wrap|Line Numbers
  1. | TOK_CMD error TOK_EOL
  2. {
  3.  
  4. const char *begin_err_str = strstr (MyLineBuffer, $1);
  5. if (begin_err_str)
  6.      // advance to the end of TOK_CMD
  7.      begin_err_str += strlen ($1);
  8.  else
  9.      // Should never happen but just in case
  10.      begin_err_str = My_Line_Buffer;
  11.  
  12. printf ("%s is not a supported command dummy\n", begin_err_str);
  13. }
Of course ther newline is still in begin-err_string but I can remove that easily as well. A bit clunky but if you cannot use the $2 on error then its all I have to go on.
Hm, indeed it's a bit 'mechanical' but it's all you can do (as far as I can tell now).
Yacc pops states until it finds your (error) state and even can skip further tokens
to work its way out of the error state: everything could've happened in the mean
time. If you got that MyLineBuffer available I think the way you outlined it is the
way to go.

Or use PCCTS or its successor ANTLR of course ;-)

kind regards,

Jos
Aug 17 '07 #4
Benny the Guard
92 New Member
Well it was a good idea, but seems to have been for naught. What happennns is the $1 does not return the full text for the token. Just part of it.

Expand|Select|Wrap|Line Numbers
  1. Here is my FLEX file (portion):
  2. ID            [[:alnum:]_\-\.\*,()#^\:\/\$\+\[\]\|\{\}@&\%\<\>]+
  3. WHITESPACE    " "+|\t+
  4. CMD                  "command"|"cmd"
  5. CMD_EXEC        ({ID}{WHITESPACE}{CMD})
  6.  
  7. %%
  8. {CMD}                {return TOK_CMD;    }
  9. {CMD_EXEC}           {return TOK_CMD;    }
and here is my YACC code:

Expand|Select|Wrap|Line Numbers
  1. | TOK_CMD error TOK_NEWLINE
  2.     {
  3.         char err_str [4098];
  4.  
  5.         char *cmd_str_ptr = strstr (Line_Buffer, $1);
  6.     if (cmd_str_ptr)
  7.           cmd_str_ptr += strlen ($1);
  8.     else
  9.           cmd_str_ptr = Line_Buffer;
  10.  
  11.         // Now remove the NEWLINE and copy locally
  12.     strncpy (err_str, cmd_str_ptr, strlen (cmd_str_ptr) - strlen ($3));
  13.         printf ("Invalid command exec: %s\n", err_str);
  14.         }
I do have TOK_CMD rules above this that have a valid/known string after it. This is the catch for CMD_EXEC that is unknown. But when I see:

command run
command run1
#PC>command run
#PC>command run2

In my test file ('run is supported') I see:

Invalid command exec: command run1
Invalid command exec: command run2

So the $1 only includes the part of the string not explicitly defined in the lexer, such as the '#PC>' part. Why does it not include the "command" portion as that is definitly part of TOK_CMD
Aug 17 '07 #5
JosAH
11,448 Recognized Expert MVP
So the $1 only includes the part of the string not explicitly defined in the lexer, such as the '#PC>' part. Why does it not include the "command" portion as that is definitly part of TOK_CMD
Because the other tokens were 'previous' tokens and all that is returned is TOK_CMD
with only the last lexical token present in (f)lex's buffer, i.e. the CMD part.
The {ID} and {WHITESPACE} tokens are already forgotten (but correctly scanned).

kind regards,

Jos
Aug 17 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3615
by: Peter Kleiweg | last post by:
Hi, I am looking for a Python equivalent of Flex. After some browsing, it seems that Plex is my best bet, but I would like to hear suggestions before I dive in. I have been using Flex a lot in combination with C programming. I never use Yacc (or Bison), though Flex and Yacc are supposed to be used together. But I found Yacc too cumbersome, and unnecessary. Yacc is about context-free grammars, nice if you
5
2694
by: Ford Desperado | last post by:
In Oracle, I would handle exceptions like this EXCEPTION WHEN OTHERS THEN -- open up an autonomous transaction -- insert SQLCODE, SQLERRM and stored procedure name into log table -- commit the autonomous transaction ROLLBACK; RAISE; END;
1
1036
by: Steve | last post by:
I have found myself being a bad programmer. I know I should trap errors and exceptions and log them, but I'm not.... everywhere. I DO when the error happens in my Form class because that is where I initially setup my error log. I find now, after adding 30+ classes that when an error happens in them, I don't do anything because I don't have easy access to the logging functionality defined in my Form class. I realize I've got a flawed...
2
2041
by: SSG | last post by:
Hai All! I am new to lex and yacc.. I compile one yacc program , but it tells errors... 10 Rules never reduced.. how to debug the code.. just i am giving my yacc sample code...
6
15485
by: Volker Hetzer | last post by:
Hi! We are finding ourselves in a situation where we have to parse several more or less free format text files. In the past, on linux, we had flex and bison for generating very fast parsers for these files. Is there any equivalent in the visual studio world? Lots of Greetings! Volker --
0
878
by: DigvijayPratap | last post by:
Hi, This is a big Problem i am struggling with it for a past couple of weeks... and now i am banging my head on th desk to get the solution...i am not using any threads in my class library..which defines the business rules for a web application...No Asynchronous Calls...No response.direct..etc.I Don get any errors in my business rules.But the mailer for reporting errors always prompts the message "Thread was being aborted ".the error message...
2
1358
by: shk253 | last post by:
Hi all - The following is the code for one of my functions in a program but the compiler is reporting errors. I've also tried declaring the variable temp within the function but the number of errors reported by the compiler increases. Here's the code just for the function with problems: char options(); { cout<<"\n\nOption 1: Fahrenheit to Celsius - Press C or 1.\n"; cout<<"Option 2: Celsius to Fahrenheit - Press F or...
11
8144
by: adramolek | last post by:
So... I'm trying to get used to using C++ ifstream (or ofstream) instead of stdio (although I'm having second thoughts). Anyways, I want to be able to display a meaningful error message if ifstream fails to open a file, but nothing I read about ifstream says anything about a reliable place to get an error message. Using stdio I can simply do: FILE *f = fopen(filename, "rb"); if (!f) perror(filename);
0
9325
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9716
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8569
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6410
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3676
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3185
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.