0

EDIT: MinC reference: https://en.wikibooks.org/wiki/MINC/Reference/MINC1-programmers-guide

Using flex and bison to write a compiler and am getting the following syntax error:

ERROR: syntax error at symbol '(' on line 9

 #include <stdio.h>
 #define MEM_SIZE  10000
 char Memory[MEM_SIZE];
 int Frame_Pointer = 0;
 int Stack_Pointer = 0;
 int Temp = 0;
 double Temp_F = 0.0;
 int main() {
(*(int *)(&Memory[Frame_Pointer +  0 ])) = (*(int *)(&Memory[Stack_Pointer + 0]));
hyde
  • 60,639
  • 21
  • 115
  • 176
MrPickles
  • 1,255
  • 1
  • 16
  • 31
  • 3
    Problem is possibly somewhere else. (A few lines before possibly). Post minimal code that exhibits the issue. – Mohit Jain Mar 14 '16 at 06:33
  • I don't think there's enough information there for us to be able to see what's up. Please see how to construct an MCVE ([MCVE]) and add a few more lines of code so that we see what's up. This code compiles OK: `int Frame_Pointer = 0; int Stack_Pointer = 0; int Memory[16]; extern void some_func(void); void some_func(void) { (*(int *)(&Memory[Frame_Pointer + 0 ])) = (*(int *)(&Memory[Stack_Pointer + 0])); }`. – Jonathan Leffler Mar 14 '16 at 06:33
  • Compiles ok for me: http://ideone.com/LlCzPi – Jonathan Potter Mar 14 '16 at 06:34
  • Still seems to work: http://ideone.com/cYpUX5 – hyde Mar 14 '16 at 06:43
  • 2
    https://en.wikibooks.org/wiki/MINC/Reference/MINC1-programmers-guide – MrPickles Mar 14 '16 at 06:44
  • 3
    What does the question have to do with minC? Is the problem that you're trying to compile a C program with something that's not a C compiler? The question also mentions "using flex and bison to write a compiler" -- how does that relate? – Paul Hankin Mar 14 '16 at 06:48
  • 1
    I'm trying to generate code for a compiler using MinC. – MrPickles Mar 14 '16 at 06:56
  • The messy cast on the final line invokes undefined behavior, since you are violating strict aliasing. Also, don't write such messy lines, split that expression in several. – Lundin Mar 14 '16 at 07:18
  • @Lundin No, that cast is technically correct: There is an explicit exception in the strict aliasing rules for `char` types that allows this cast. Nevertheless, I heartily agree with you that such messy casts are a no-no. – cmaster - reinstate monica Mar 14 '16 at 17:39
  • The only thing I find missing in your code, is a closing `}` for the `main()` function. – cmaster - reinstate monica Mar 14 '16 at 17:40
  • @cmaster No... the exception is when converting _to_ a character type, not _from_ a character type. The strict aliasing rule goes like "An object shall have its stored value accessed only by an **lvalue** expression that has one of the following types: ..." Lvalue meaning the left operand of the assignment. [See this](http://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule) for more info. – Lundin Mar 15 '16 at 07:18
  • @Lundin That's a misunderstanding of what an lvalue is: The name comes from the term "locator value", and signifies any expression that has an address in memory. This is a prerequisite to assignment, but also for address taking and such. The use of lvalue in strict aliasing rules only means that you do `*(Foo*)pointer` instead of `(Foo)*pointer`. – cmaster - reinstate monica Mar 16 '16 at 07:02
  • @cmaster It doesn't matter, the expression here converts from a pointer type to another pointer type _which doesn't meet the requirements_ of 6.5/7. – Lundin Mar 16 '16 at 07:16
  • @Lundin With the danger of being pedantic: The cast converts a pointer to `char` to a pointer to `int`. Reads and writes to these two pointers have to preserve order according to the exception in the strict aliasing rules. However, we don't see any other read/write here. But, if the part of the char array that is here read/written as an `int` is only ever accessed via a pointer to `int` (or via a pointer to `char`), the code complies to strict aliasing rules. The relevant thing to look at is the type via which a value is accessed; all accesses via this type have to retain order. – cmaster - reinstate monica Mar 17 '16 at 21:13

1 Answers1

1

Problem is almost certainly in the previous line, and syntax error is about the first (.

Problem could even be in an include file, if there's #include just before that line (and since it is line 9, that is quite possible).

If you have trouble figuring out the exact problem, it may help to get the preprosessor output (before actual compilation) and examine that (see here for how to do it with gcc).

Community
  • 1
  • 1
hyde
  • 60,639
  • 21
  • 115
  • 176