2
votes

main.c (forgot the ";" on purpose)

#define MULTI_LINE_DEFINE int x = 1;\
int y = 2;\
int z = 3\
int v = 4;

void main()
{
  MULTI_LINE_DEFINE

  int w = 10;
}

gcc -E main.c -omain.pp

main.pp

# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.c"





void main()
{
  int x = 1;int y = 2;int z = 3int v = 4;

  int w = 10;
}

main.c:8:3: error: invalid suffix "int" on integer constant
main.c: In function 'main':
main.c:8: error: expected ',' or ';' before 'v'

how did the compiler know if the error was at "line 8" of main.cpp if it was fed the output of the preprocessor ("main.pp")?

I was expecting "#line ..." directives in the output of preprocessor (main.pp)

I am using yacc and lex to simulate gcc behaviour but on my own language.

file1 => lex1+yacc1 => file2 => lex2+yacc2 => file3

Is it possible to emulate gcc like this in lex and yacc then?

file2 corresponds to ouput of gcc preprocessor

file3 corresponds to output of gcc compiler

I want errors in file 2 to reference line numbers in file1

1
Have you tried to run the preprocessor only?Eugene Sh.
How could it not know? There is no #line and no macro.Hans Passant
Preprocessing is part of and integrated into the compiler. The compiler isn't literally reading text files produced by the preprocessor.melpomene
The lines starting # 1 are treated as though they were #line directives.rici
The blank lines are so that the line count works out correctly. The preprocessor directives are replaced with blank lines, instead of being deleted, so that subsequent lines will still have the right line numbers.rici

1 Answers

0
votes

You're misinterpreting the error message:

main.c:8: error: expected ',' or ';' before 'line4'

In this code:

# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.c"





void main()
{
  int line1 = 1;int line2 = 2;int line3 = 3int line4 = 4;  <- THIS variable

  int line10 = 10;
}

The "'line4'" in the error message refers to the variable line4, not the 4th line of the file.