-2

My target is to implement a very simple lexical analysis of the C language, such as when you read an expression ab=3, then It will be analysed as:

ID<ab> OP<=> LIT<8>   (op will be "operator",LIT will be "literal"). 

There is also an situation when you read an expression a ? b :c. normally it will be analysed as

ID<a> SEP<?> ID<b> SEP<:> ID<c>". 

but as we know It actually is the ternary operator. So the analysis listed above is not correct.

Now I just want to refer to a compiler such as gcc, g++ and check how do these compilers implement the lexical analysis? Anyone who can give me an suggestion ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ryu
  • 651
  • 9
  • 23
  • So if you want it as an operator why are you scanning it as a separator? What's the question here? – user207421 Nov 02 '13 at 08:51
  • Because I got this resource from others. Both "?" and ":" are processed as separator by default. – ryu Nov 03 '13 at 13:30

1 Answers1

0

What makes that lexer output incorrect? You are thinking too far ahead and trying to solve parse problems in the lexer. If you really like, you could change ? to be an operator (as it should be in this case), but ultimately it doesn't matter, just parse using the terminals you have defined as tokens.

Tim Wakeham
  • 1,029
  • 1
  • 8
  • 12
  • Yes, you are right. Because I am new to this project, still can't distinguish the usage of lexical and syntax analyzer. By this time, It's still a little earlier to resolve this kind of issue. I will check this again later. – ryu Nov 03 '13 at 13:25