Bison %code top error - yacc

%code top command doesn't include its contents in parser.tab.h file (It should do so, right?). Bison version is 2.4.1. What is the problem with this (simplified) code?
%{
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <io.h>
#define YYDEBUG 0
int errors;
%}
%code top {
struct DICT
{
char *Name;
int Offs;
int Size;
struct DICT *Next;
};
typedef struct DICT DICT;
struct NODE
{
int ID;
int Value;
DICT *Var;
struct NODE *Left;
struct NODE *Right;
};
typedef struct NODE NODE;
}
%{
NODE *Tree = 0;
NODE *Node(int ID, int Value, DICT *Var, NODE *Left, NODE *Right);
void yyerror(char *s)
{
errors++;
printf("%s\n", s);
}
%}
%no_lines
%union
{
int Value;
char *ID;
NODE *Node;
}
EDIT:
with "%code requires" problem was resolved but another arise:
parser.tab.h:40: error: redefinition of 'struct DICT'
parser.tab.h:47: error: redefinition of typedef 'DICT'
parser.tab.c:145: error: previous declaration of 'DICT' was here

Using %code top will not insert the code into the header but only into the source file. It is well documented here.
I guess %code provides (or %code requires) will be more suited because it inserts the definitions in both source and header file.

Related

How can I TryParse userinput?

this is my problem:
#include "stdafx.h"
#include <iostream>
#include <string>
#define newline '\n'
using namespace std;
using namespace System;
int _tmain(int argc, _TCHAR* argv[])
{
bool isNumber;
String^ age;
Int32 result;
cout
<< "Please enter your age:"
<< newline;
cin >> age;
isNumber = Int32::TryParse(age, result);
return 0;
}
I cant use cin with a String^ and I can't use string to tryparse

PImpl template in Objective-C

I'm trying to use the PImpl idiom to use the <vector> libray in my Objective-C project.
I have managed to implement it, but it only for the Int type as you can see in my .h and .mm file.
file.h
#import <Foundation/Foundation.h>
struct Vector_impl;
#interface MM_Matrix : NSObject
{
struct Vector_impl *MMVec;
}
#property unsigned rows, columns;
- (MM_Matrix *) initwith:(int) n and: (int) m;
- (long) size: (int) n;
file.mm
#import "MM_Matrix.h"
#include <vector>
struct Vector_impl
{
std::vector<std::vector<int>> matrix;
};
#implementation MM_Matrix : NSObject
- (MM_Matrix *) initwith:(int) n and:(int)m
{
[self setRows:n];
[self setColumns:m];
MMVec = new Vector_impl;
MMVec->matrix.resize(n);
for (int i=0; i<n; i++) {
MMVec->matrix[i].resize(m, 0);
}
return self;
}
- (long) size: (int) n
{
return MMVec->matrix[n].size();
}
#end
I would like to implement a generic type (template maybe?) as you would do in the <vector> library, but I have no idea how to achieve that.
Assuming that you want to template std::vector<std::vector<int>>: there are two possibilities:
Use a weak type, e.g. void* and take care of conversion inside the class.
Implement an interface for every type you want to support.
For the second option it is enough to instantiate the template class for every type, so that the compiler knows which types are used.
Also have a look at this related question: pimpl for a templated class.

Using extern for static variables

I'm seeing something odd when I run some code that uses the extern keyword to reference a static variable within the implementation file. So I declare the static variable gCounter within my implementation file and reference it within two methods in the same implementation file (because its static). However, when i use the extern keyword in my methods i get different results. My understanding (from reading my book) is that extern isn't necessary when you're referencing a static variable declared in the same file as your methods. Code is as follows:
/** interface **/
#import <Foundation/Foundation.h>
#interface Fraction : NSObject
+(Fraction *) allocF;
+(int) count;
#end
/**implementation**/
#import "Fraction.h"
static int gCounter;
#implementation Fraction
+(Fraction *) allocF
{
extern int gCounter;
++gCounter;
return [Fraction alloc];
}
+(int)count
{
extern int gCounter;
return gCounter;
}
#end
/**main**/
#import "Fraction.h"
int main (int argc, const char * argv[])
{
#autoreleasepool
{
Fraction *a, *b, *c;
NSLog(#"The number of fractions allocated: %i", [Fraction count]);
a = [[Fraction allocF] init];
b = [[Fraction allocF] init];
c = [[Fraction allocF] init];
NSLog(#"The number of fractions allocated: %i", [Fraction count]);
}
return(0);
}
When I use the extern keyword in my methods, the code works properly and results in the integer 3 being printed. However, when I remove extern, the integer 2 gets printed. Why is that? Since gCounter is a static variable, shouldn't this work without the extern keyword?
You need to understand the difference between a declaration and a definition:
static int x and int x are definitions. The compiler reserves memory for x.
extern int x on the other hand is a declaration. You tell the compiler that there is a variable x that is defined somewhere else.
Also, you can define different variables in different scopes, that have the same variable name:
int x = 0;
{
int x = 1;
NSLog(#"x = %d", x); // x = 1
}
NSLog(#"x = %d", x); // x = 0
So if you write
int x;
void foo() {
int x;
x++;
}
you are incrementing the function local x.
int x;
void foo() {
x++;
}
increments the global x
int x;
void foo() {
extern int x;
x++;
}
You need to declare extern int x if your definition of x is in another compilation unit, if it's in the same compilation unit, the last two are equivalent.

Calling Objective-C method from imp

I'm trying to get a Method at runtime and then use its data structure to call it's implementation. Just to clarify, this is for learning purposes, not for any practical reason. So here is my code.
#import <Foundation/Foundation.h>
#import <stdio.h>
#import <objc/runtime.h>
#interface Test : NSObject
-(void)method;
#end
#implementation Test
-(void)method {
puts("this is a method");
}
#end
int main(int argc, char *argv[]) {
struct objc_method *t = (struct objc_method*) class_getInstanceMethod([Test class], #selector(method));
Test *ztest = [Test new];
(t->method_imp)(ztest, t->method_name);
[ztest release];
return 0;
}
The definition of struct objc_method is as follows (defined in objc/runtime.h)
typedef struct objc_method *Method;
....
struct objc_method {
SEL method_name OBJC2_UNAVAILABLE;
char *method_types OBJC2_UNAVAILABLE;
IMP method_imp OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
however when I try to compile my code, I get this error.
error: dereferencing pointer to incomplete type
But when I add this to my code (to explicitly declare an objc_method), it works just as expected.
struct objc_method {
SEL method_name;
char *method_types;
IMP method_imp;
};
typedef struct objc_method* Method;
Could someone explain to me why my code works when I explicitly declare this structure, and not when I import it from objc/runtime.h? Does it have anything to do with OBJC2_UNAVAILABLE? I can't find a definition for that, but it is defined in my environment.
EDIT:
I ran gcc -E code.m -o out.m to see what OBJC2_UNAVAILABLE was getting replaced with, it turns out that OBJC2_UNAVAILABLE was defined as __attribute__((unavailable)) in my environment. Can someone explain what that means and why Method still works if this structure is "unavailable"?
I just took a good look at the objc runtime header and found what the problem is, and how to fix it :)
So if you look at the area of the file that contains the transparent definition of the structure, you'll see that it's in the body of:
#if !__OBJC2__
...
#endif
Since this IS defined, it means that the structure that we refer to is in fact forward-declared and opaque (and thus incomplete).
What we must do instead is use the functions provided for accessing these members:
IMP method_getImplementation(Method method);
SEL method_getName(Method method);
void method_getReturnType(Method method, char *dst, size_t dst_len);
Etc.
You can see the full list of accessor methods and plenty of other goodies in the Runtime Reference Guide.
Hope it helps!
Its fields were previously defined, but it's an opaque type in ObjC-2. Use the runtime instead, and do not define the fields yourself:
int main(int argc, char *argv[]) {
Method t = class_getInstanceMethod([Test class], #selector(method));
Test * ztest = [Test new];
IMP imp = method_getImplementation(t);
typedef void (*fn)(id,SEL);
fn f = (fn)imp;
f(ztest,#selector(method));
[ztest release];
return 0;
}

Objective-C extern and typedef undefined symbols

I am new to objective-c. I have the following:
#interface HPSEnumerations : NSObject
typedef NSString* HPS_FORMELEMENTTYPE;
extern HPS_FORMELEMENTTYPE Textfield;
extern HPS_FORMELEMENTTYPE Label;
extern HPS_FORMELEMENTTYPE ImageView;
#end
The compiler gives errors with:
Undefined symbols for architecture i386:
"_ImageView", referenced from:
Can anyone explain what's happening, and how to fix it? My basic requirement is to have an enumeration where I have string values instead of ints - this looked about as close as I could get.
extern declaration does not create variable, it is just a promise that variable is created somewhere outside of the current module. To fix your error you need to actually create it in some implementation file (that is declare it without 'extern' word):
// Header
// HPSEnumerations.h
extern HPS_FORMELEMENTTYPE TextfieldType;
extern HPS_FORMELEMENTTYPE LabelType;
extern HPS_FORMELEMENTTYPE ImageViewType;
// Implementation
// HPSEnumerations.m
HPS_FORMELEMENTTYPE TextfieldType = #"Textfield";
HPS_FORMELEMENTTYPE LabelType = #"Label";
HPS_FORMELEMENTTYPE ImageViewType = #"ImageView";
P.S. I've also changed enumeration names a bit and there's no sense to put your "enumeration" into obj-c interface declaration