tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] A TCC C parser question about the grammar


From: Wei
Subject: Re: [Tinycc-devel] A TCC C parser question about the grammar
Date: Sun, 6 Sep 2009 11:18:12 +0800

Hi, Fred,

1)

For the C standard, I think you can refer to the C grammar page in the ANTLR website:
http://www.antlr.org/grammar/1153358328744/C.g

I have checked this ANTRL C grammar file with the C standard PDF file,
for the "assignment_expression" & the "multiplicative_expression",
the productions of the 2 in this 2 files are equivalent.
So I think you can refer to that page for the C standard grammar.

And you will see that the C grammar used by TCC is different from the standard C grammar, and I don't know why.

I have tried many grammar transformations I know to transform the C standard grammar to the one TCC used, however, all tries have failed.

It seems that the C standard grammar to break the "assignment_expression" to many smaller "assignment_expression", but TCC tries to break the "assignment_expression" to "conditional_expression".

It seems that the C standard grammar to break the "multiplicative_expression" to many "cast_expression", but TCC tries to break the "multiplicative_expression" to many "assignment_expression" !!!

2)

The following program you mentioned does be compiled by TCC (however, it is wrong).
========================
#include <stdio.h>

int
main() {
  int x, y;
 
  x = 3;
  y = 3 * x = 2;
 
  printf("x = %d, y = %d\n", x, y);
 
  return 0;
}
=======================

And the output from the executable is

=======================
x = 2, y = 6
=======================


Wei Hu
http://www.csie.ntu.edu.tw/~r88052/
http://wei-hu-tw.blogspot.com/


2009/9/6 Fred Weigel <address@hidden>
Wei

Not looking at the standard right now, but the following is valid:

x = 1, 2, 3; // x gets 3

A test for the second case would be

x = 3;
y = 3 * x = 2;

This should be an error, because 3 * x is not an lvalue.

Does this compile with tcc? (don't know, the machine I am on doesn't
have the compiler).


On Sat, 2009-09-05 at 01:05 +0800, Wei wrote:
Hi, TCC stackholders,

I am tracing TCC's source codes, and find something I don't understand.
What I don't understand is about the grammar.

From the C89 or C99 standard:

======================================================================
assignment_expression
: lvalue assignment_operator assignment_expression
| conditional_expression
;
======================================================================

However, in TCC,

======================================================================
static void gexpr(void)
{
while (1) {
expr_eq();
if (tok != ',')
break;
vpop();
next();
}
}
======================================================================

and expr_eq( ) seems to parse C conditional _expression_ (ie, xxx ? xxx : xxx).
It seems that it doesn't parse codes like "a = b = c = d = 4;".

And in the C standard,

======================================================================
multiplicative_expression
: (cast_expression) ('*' cast_expression | '/' cast_expression | '%' cast_expression)*
;
======================================================================

However, in TCC, the function to parse "multiplicative_expression" is expr_prod( ):

======================================================================
static void expr_prod(void)
{
int t;

uneq();
while (tok == '*' || tok == '/' || tok == '%') {
t = tok;
next();
uneq();
gen_op(t);
}
}
======================================================================

and uneq( ) seems to parse the so-call "assignment_expression.", not the standard-defined "cast_expression"

Why TCC uses such a grammar? it seems not compatible with the C standard.

Wei.


_______________________________________________
Tinycc-devel mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/tinycc-devel

_______________________________________________
Tinycc-devel mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/tinycc-devel



reply via email to

[Prev in Thread] Current Thread [Next in Thread]