I have the following:
FileReader.cpp
#include "FileReader.h"
#include "FileData.h"
using namespace System;
using namespace System::IO;
FileReader::FileReader(){}
System::Void FileReader::readFile(System::String^ fileName) {
this->_fileName = fileName;
Console::WriteLine("Opening: " + _fileName);
try {
StreamReader^ fileIn = File::OpenText(_fileName);
System::String^ input;
int count = 0;
while ((count < 10 && (input = fileIn->ReadLine()) != nullptr)) {
Console::WriteLine(input);
count++;
}
}
catch (FileNotFoundException^ e) {
Console::WriteLine("FileNotFound");
}
}
FileReader.h
#pragma once
ref class FileReader
{
public:
FileReader();
FileData^ fData = gcnew FileData(); // This is whats causing the errors.
System::String^ _fileName;
System::Void readFile(System::String^ fileName);
};
FileData.cpp
#include "FileData.h"
FileData::FileData(){}
System::Void FileData::Set_DataName(System::String^ dName)
{
this->dataName= dName;
}
FileData.h
#pragma once
using namespace System;
ref class FileData
{
public:
FileData();
System::String^ dataName;
System::Void Set_PlayerName(System::String^ dName);
};
Problem I'm having is the compiler returns big list of errors when trying to create the FileData object in FileReader.h
Error C2143 syntax error: missing ';' before '^'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2143 syntax error: missing ';' before '^'
Error C4430 missing type specifier - int assumed.
Error C2238 unexpected token(s) preceding ';'
Error C2143 syntax error: missing ';' before '^'
Error C4430 missing type specifier - int assumed.
Error C2238 unexpected token(s) preceding ';'
I know its probably something simple I've missed but I can't figure out what I've done wrong. Anyone able to help?
Related
So basically, in my bison file if yyparse fails (i.e there is syntax error) I want to print 'ERROR' statement and not print anything else that I do in the above part of bison file in fac the stmt part. if yyparse returns 1 is there any way to skip the content of middle part of bison file? such as idk maybe writing if statement above the stmt part etc? I would appreciate any help! Thanks in advance.
Such as :
%{
#include "header.h"
#include <stdio.h>
void yyerror (const char *s)
{}
extern int line;
%}
%token ...///
%// some tokens types etc...
%union
{
St class;
int value;
char* str;
int line_num;
float float_value;
}
%start prog
%%
prog: '[' stmtlst ']'
;
stmtlst: stmtlst stmt |
;
stmt: setStmt | if | print | unaryOperation | expr
{
if ($1.type==flt && $1.line_num!=0) {
printf("Result of expression on %d is (",$1.line_num);
printf( "%0.1f)\n", $1.float_value);
$$.type=flt;
}
else if ($1.type==integer && $1.line_num!=0){
$$.type=integer;
printf("Result of expression on %d is (%d)\n",$1.line_num,$1.value);
}
else if ($1.type==string && $1.line_num!=0) {
$$.type=string;
printf("Result of expression on %d is (%s)\n",$1.line_num,$1.str);
}
else if ($1.type==mismatch && $1.line_num!=0)
{
$$.type=mismatch;
printf("Type mismatch on %d \n",$1.line_num);
}
else{ }
}
%%
int main ()
{
if (yyparse()) {
// if parse error happens only print this printf and not above stmt part
printf("ERROR\n");
return 1;
}
else {
// successful parsing
return 0;
}
}
Unfortunately, time travel is not an option. By the time the error is detected, the printf calls have already happened. You cannot make them unhappen.
What you can so (and must do if you are writing a compiler rather than a calculator) is create some data structure which represents the parsed program, instead of trying to execute it immediately. That data structure -- which could be an abstract syntax tree (AST) or a list of three-address instructions, or whatever, will be used to produce the compiled program. Only when the compiled program is run will the statements be evaluated.
for an if and if-else statement grammar I have some grammar.
The following is a simplified excerpt from my code to show how I have the if and if-else statement grammar, so if there are errors unrelated to that grammar then don't worry about it. I assure you there are no compilation errors in the code I am using:
%token IF ELSE VOID ID VOID_PARAMS
%nonassoc shift_else
%nonassoc ElSE
%%
Func: VOID ID VOID_PARAMS '{' Stmt '}'
;
If_Stmt: IF '(' L_expr ')' Stmt
;
Stmt: If_Stmt shift_else
;
| If_Stmt ELSE Stmt
;
| ';'
| ...
;
L_expr: ...
;
It has been working just fine for a while, but now it is finding errors when it reaches the end of a function. For example:
void foo(void) {
if (1 > 5)
;
}
gives this output ( using yyerror() ):
Found unexpected token: '}' on line 4
Any suggestions as to why this could be happening? And what can I do to fix this?
After reading if(1 > 5) ; the parser is expecting an ELSE or a shift_else.
Presumably there's not an ELSE there, since it doesn't appear in the source code.
Unless your lexer conjures a shift_else out of thin air, the next token will be a }, which is not an ELSE or shift_else, hence the error.
I'm getting the following errors, hopefully someone can explain these errors for me as I'm not very good in C at the moment.
case ' ':
This is the error here. shellNew.c:57: error: a label can only be part of a statement and a declaration is not a statement
int rCheck = 0;
shellNew.c:58: error: expected expression before â
int foundPos = 0;
while(rCheck < 10)
{
if(inputBuffer[2] == historyBuffer[rCheck][0])
{
shellNew.c:63: error: â undeclared (first use in this function)
shellNew.c:63: error: (Each undeclared identifier is reported only once
shellNew.c:63: error: for each function it appears in.)
foundPos = rCheck;
}
rCheck++;
}
if(rCheck == 0)
{
printf("There was no command matching your criteria\n");
}
else
{
strcpy(inputBuffer, historyBuffer[rCheck]);
}
break;
Assuming that there was a switch in the previous 56 lines of code, then the compiler is complaining that you can't do:
switch (variable)
{
case ' ':
int var = 23;
because the declaration doesn't count as a statement and labels must be attached to statements. Converted into a minuscule function, this code gave me the error you report with GCC 4.7.1 on Mac OS X 10.7.5. The subsequent errors are probably because your variable rCheck was not declared because of the misplaced label, leading to problems where you try to use it.
You can't skip over variable declarations, so you'll need to use a statement block inside the switch statement block:
switch (variable)
{
case ' ':
{
int var = 23;
...
}
break;
}
This code compiled cleanly. It's a moot point whether it's better to put the break inside the statement block or outside; they're equivalent.
I'm trying to write a simple stack code and I get this code from a data structure book but it fails when i try to compile.
bool pushStack (STACK* stack, void* dataInPtr)
{
STACK_NODE* newPtr;
newPtr = (STACK_NODE*) malloc(sizeof(STACK_NODE));
if(!newPtr)
return FALSE;
newPtr->dataPtr = dataInPtr;
newPtr->link = stack->top;
stack->top = newPtr;
(stack->count)++;
return TRUE;
}
For example for this code, compiler says
Error 1 error C2061: syntax error : identifier 'pushStack'
Error 2 error C2059: syntax error : ';'
Error 3 error C2059: syntax error : 'type'
How can we resolve this? I tried to change TRUE to true, but it's not worked.
C doesn't have a bool data type (C++ does, though). Have the function return an int, and return 1 for TRUE and 0 for FALSE. Alternatively, #DEFINE TRUE 1 and #DEFINE FALSE 0.
I get several compile errors if I compile the following function in Visual Studio 2005:
void search()
{
deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice");
BTUINT32 deviceClass = 0; // 0 represents all classes
BTUINT16 maxDevices = 200; // 0 represents an unlimited number of responses
BTUINT16 maxDuration = 45; // maxDuration * 1.28 = number of seconds
Btsdk_StartDeviceDiscovery(deviceClass, maxDevices, maxDuration);
WaitForSingleObject(deviceEventHandle, INFINITE);
if (deviceEventHandle != NULL) {
CloseHandle(deviceEventHandle);
deviceEventHandle = NULL;
}
}
These are the errors I get:
error C2275: 'BTUINT32' : illegal use of this type as an expression
error C2146: syntax error : missing ';' before identifier 'deviceClass'
error C2065: 'deviceClass' : undeclared identifier
error C2275: 'BTUINT16' : illegal use of this type as an expression
error C2146: syntax error : missing ';' before identifier 'maxDevices'
error C2065: 'maxDevices' : undeclared identifier
error C2275: 'BTUINT16' : illegal use of this type as an expression
error C2146: syntax error : missing ';' before identifier 'maxDuration'
error C2065: 'maxDuration' : undeclared identifier
If I comment out the line containing the CreateEvent call the code compiles without errors:
void search()
{
//deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice");
BTUINT32 deviceClass = 0; // 0 represents all classes
BTUINT16 maxDevices = 200; // 0 represents an unlimited number of responses
BTUINT16 maxDuration = 45; // maxDuration * 1.28 = number of seconds
Btsdk_StartDeviceDiscovery(deviceClass, maxDevices, maxDuration);
WaitForSingleObject(deviceEventHandle, INFINITE);
if (deviceEventHandle != NULL) {
CloseHandle(deviceEventHandle);
deviceEventHandle = NULL;
}
}
These are the headers I use:
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include "Btsdk_ui.h"
I compile the code as C code (/TC) in Visual Studio 2005. The "Btsdk_ui.h" file is part of the BlueSoleil bluetooth stack and inlcudes another file that contains definitions of BTUINT32 and BTUINT16.
All suggestions are welcome.
In C, you declare all your variables at the beginning of the block.
Move your deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice"); to after your block of BTUINT variables.
When compiling C code, MSVC doesn't allow declarations to be mixed with statements - declarations can only be at the start of a block (MSVC follows more closely to the ANSI/ISO C90 standard than the C99 standard).
Try:
void search()
{
BTUINT32 deviceClass = 0; // 0 represents all classes
BTUINT16 maxDevices = 200; // 0 represents an unlimited number of responses
BTUINT16 maxDuration = 45; // maxDuration * 1.28 = number of seconds
deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice");
Btsdk_StartDeviceDiscovery(deviceClass, maxDevices, maxDuration);
WaitForSingleObject(deviceEventHandle, INFINITE);
if (deviceEventHandle != NULL) {
CloseHandle(deviceEventHandle);
deviceEventHandle = NULL;
}
}