How can I customize my error message in yacc/lex project to display the line and type of problem? [closed] - project

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am new in programming but I have a yacc/lex project and some difficulty in writing the error message in detail (line and type of error). Any help with a small example, please.

Add the following to your flex scanner definition, to cause the scanner to track line numbers:
%option yylineno
(See the flex manual.)
Then add the following declarations to your bison grammar:
%define parse.error verbose
%define parse.lac full
(See the bison manual chapters on error reporting and LAC (lookahead correction).
Finally, use a definition of yyerror which uses the line number information. At a minimum, something like:
void yyerror(const char* msg) {
fprintf(stderr, "At line %d: %s\n", yylineno, msg);
}

Ask bad questions get correct but uninformative answers
void yyerror(const char *s);
extern int line_num;
void yyerror(const char *s)
{
std::cerr << "PARSING ERROR: " << line_num << " " << s << std::endl;
exit(1);
}

Related

CDT inserter and extractor operator problem

this question is a continuing of Error on Constrained Delaunay Triangulation and Gabriel Triangulations
Trying to write a minimal example for that problem I planned insert the triangulation
in a file (std::ofstream) using the << operator of CDT without calling the CGAL::make_conforming_Delaunay_2(cdt); or CGAL::make_conforming_Gabriel_2(cdt),
knowing that until this point everything occured OK.
The triangulation was created without problems and before the exit of the aplication I saved the triangulation in a file using the << operator of CDT. The file was saved without error.
When I tried to read the file using the >> operator of CDT an exception was raised (inside the >> operator). The text of exception is:
CGAL ERROR: assertion violation!\nExpr: s == LEFT_TURN\nFile: C:\\dev\\CGAL-5.3.1\\include\\CGAL\\Triangulation_2.h\nLine: 919
The code I wrote:
int main()
{
CDT cdt;
std::ifstream ArqSuperficie("trian.dtr"); This file was created with << operator of CDT
if (ArqSuperficie.good() == false)
{
return 1;
}
try()
{
ArqSuperficie >> cdt;
}
catch(std::exception& e)
{
std::cerr << "ERROR: exception " << e.what() << std::endl;
}
return 0;
}
Why CDT cannot read a file created by itself, of a triangulation that was created by itself without errors?
Based on the message of the exception I can´t have a clue of what is happening...
Thank you

Command Line Calculator in Objective C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to implement a command line application in C/Objective C which will act as a calculator of more than two numbers.
E.g ./calc 5 + 4 * 6
= 29
I just need an idea or simple algorithm to start. I will appreciate any help on this.
The algorithm you want is the infix notation to postfix notation converter.
You can find some more info on it over here.
http://scriptasylum.com/tutorials/infix_postfix/algorithms/infix-postfix/index.htm.
EDIT: I am not sure if this will help, but here is an implementation in Java. I'm not familiar with Objective-C
// converts a infix string to postfix string
private void convertInfixToPostfix(){
// create an empty operand stack
operatorStack = new Stack<>();
Operator operator = null;
Operand operand = null;
for(int i = 0; i < expressionTokens.size(); i++){
String token = expressionTokens.get(i);
Element element = new Element(token);
if(element.isOperand(token)){ // check if element is operand
// add the element to the postfix string
operand = new Operand(element.getStringValue());
postFixString.add(operand);
}
else if(operatorStack.isEmpty()){
// push the token to the operator stack, its an operator
operator = new Operator(element.getStringValue());
operatorStack.push(operator);
}
else {
operator = new Operator(element.getStringValue());
while(!operatorStack.isEmpty() &&
(operatorStack.peek().getPrecedence()
<= operator.getPrecedence()))
postFixString.add(operatorStack.pop());
operatorStack.push(operator);
}
}
// add the rest of the operator stack to the postfix string
while(!operatorStack.isEmpty()){
Operator remainingOperator = operatorStack.pop();
postFixString.add(remainingOperator);
}
}

Need tutorial on CAN protocol [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I have been assigned a project in my lab to implement CAN protocol on ARM 7.
I looked for some tutorials and sample code, but all looks so much complex that I think I should get some help on the coding part. Can anybody explain me the basic transmitter and receiver coding on any ARM board?
The sender code is the following. I have used question marks where I don't understand the full meaning of an expression.
#include <lpc23xx.h>
#include "type.h
#include "can.h"
#include <LPC23xx.H>
CAN_MSG MsgBuf_RX1,MsgBuf_RX2; // TX and RX Buffers for CAN message
volatile DWORD CAN1RxDone, CAN2RxDone;
int main(void)
{
DWORD tempbuf1,tempbuf2;
int current;
FIO2DIR=0x000000FF;
CAN_Init(BITRATE100K28_8MHZ);
MsgBuf_RX2.Frame = 0x0;
MsgBuf_RX2.MsgID = 0x0;
MsgBuf_RX2.DataA = 0x0;
MsgBuf_RX2.DataB = 0x0;
CAN_SetACCF(ACCF_BYPASS);
while (1)
{
while (!(CAN2GSR & (1 << 0)) )
;
if (CAN2RxDone == TRUE)
{
tempbuf1 = MsgBuf_RX2.DataA; // Data A has 32 bits, of which only the
// first 16 bits are actual data
tempbuf2 = (tempbuf1 & 0x0000ffff); //??
current = tempbuf2;
if ((current/3) >= 25)
FIO2SET |= 0x00000001; ///??
}
CAN2RxDone = FALSE;
if (MsgBuf_RX2.Frame & (1 << 10)) //?
{
MsgBuf_RX2.Frame &= ~(1 << 10); //?
}
}
}
Keil provides some examples and programs for CAN development: http://www.keil.com/dd/vtr/4152/7837.htm
Here you can find CAN source for LPC2129: http://www.siwawi.arubi.uni-kl.de/avr_projects/arm_projects/
Here some examples: http://mbed.org/handbook/CAN

Code example for a TLM fifo [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am new to TLM.
Someone can give me an example code for connecting two processes by a TLM fifo?
Thank you
I searched in doulos, but I only saw examples of sockets.
Someone helped me and I leave here a code example of a tlm fifo
#include "systemc"
#include "tlm.h"
// PRODUCER 1
SC_MODULE(producer)
{
sc_core::sc_port< tlm::tlm_fifo_put_if<int> > out; //FIFO OUT
SC_CTOR(producer)
: out("out")
{
SC_THREAD(run); //função
}
void run()
{
int i = 42;
std::cout << name() << ": " << i << std::endl;
out->put(i);
}
}; // producer
// CONSUMER
SC_MODULE(consumer)
{
sc_core::sc_port< tlm::tlm_fifo_get_if<int> > in;
SC_CTOR(consumer)
: in("in")
{
SC_THREAD(run); //função
}
void run()
{
int i = in->get();
std::cout << name() << ": " << i << std::endl;
}
}; // consumer
// MAIN
int sc_main(int, char*[] )
{
tlm::tlm_fifo<int> fifo("fifo");
producer prod("producer");
prod.out(fifo);
consumer cons("consumer");
cons.in(fifo);
sc_core::sc_start();
char myLine[100];
cin.getline(myLine, 100);
return 0;
}
Thank you
there are some good examples in doulos website.

How do I get user input without using scanf in a console app?

I'm trying to allow users to simply hit Enter without typing anything, and use this to mean accepting a default value. scanf isn't doing what I want and the app still 'blocks': the next line of code doesn't run.
The only way is to actually type something THEN press Enter.
I tried using NSFileHandle and fileHandleWithStandardInput; however, it seems that the user is now forced to hit Ctrl-D to indicate EOF.
Someone suggested using fgets, but I cannot work out what to pass as 3rd parameter (of FILE* type). Tried stdin but it doesn't 'block'.
How do I accept input from a user, using Objective-C, and at the same time allow the user to simply hit Enter without being forced to type anything? How do I read a single line, even if that line is blank?
Assuming the code doesn't block and the next line runs immediately (as you seemed to indicate early in the question and in a comment), you have a common problem when mixing non-line-based and line-based input.
What happens is you have a newline left in the buffer, and fgets sees that, reads it, and returns, instead of doing what you really want: ignoring it, and then reading a line.
The solution is to simply do the ignoring part yourself, and then call fgets:
#include <stdio.h>
#include <string.h>
FILE* ignoreline(FILE* stream) {
for (int c; (c = fgetc(stream)) != EOF;) {
if (c == '\n') break;
}
return stream;
}
void example_use() {
char buf[1000];
ignoreline(stdin);
fgets(buf, sizeof buf, stdin);
// or, since it returns the stream, can be more compact:
fgets(buf, sizeof buf, ignoreline(stdin));
}
int main() { // error handling omitted
int n;
printf("Enter a number: ");
scanf("%d", &n);
char buf[1000];
printf("Enter a line: ");
ignoreline(stdin); // comment this line and compare the difference
fgets(buf, sizeof buf, stdin);
*strchr(buf, '\n') = '\0';
printf("You entered '%s'.\n", buf);
return 0;
}
Note that it is also common and encouraged to "pair" the ignoreline with the scanf (or other non-line-based input) to turn that into line-based input. You may want to modify it, in that case, so you can tell the difference between input of "42 abc" and "42" (in the "Enter a number" case). Some people just use fgets everywhere, then parse that line with sscanf, and while that works, it's not necessary.
I use getch(); in library conio.h
simply the program waits for any key to be pressed
If you're using Windows, you can use the ReadConsoleInput function (see MSDN for more on this) :
INPUT_RECORD keyin;
DWORD r;
while (ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE),&keyin,1,&r)) {
if (keyin.EventType!=KEY_EVENT) continue;
if (keyin.Event.KeyEvent.wVirtualKeyCode==VK_SPACE) break; ///use these VK codes to get any key's input
if (keyin.Event.KeyEvent.wVirtualKeyCode==VK_F1)
{
printf("You pressed F1\n");
}
if (keyin.Event.KeyEvent.wVirtualKeyCode==VK_F2)
{
printf("You pressed F2\n",);
}
}//end while loop
You don't need to hit enter after each key then.This works like a dream for me...
use getchar() to take input without using scanf function...