Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the compiler spend most of its time during parsing?

I read in Sebesta book, that the compiler spends most of its time in lexing source code. So, optimizing the lexer is a necessity, unlike the syntax analyzer.

If this is true, why lexical analysis stage takes so much time compared to syntax analysis in general ?

I mean by syntax analysis the the derivation process.

like image 398
Khaled Alshaya Avatar asked Jan 19 '26 09:01

Khaled Alshaya


1 Answers

First, I don't think it actually is true: in many compilers, most time is not spend in lexing source code. For example, in C++ compilers (e.g. g++), most time is spend in semantic analysis, in particular in overload resolution (trying to find out what implicit template instantiations to perform). Also, in C and C++, most time is often spend in optimization (creating graph representations of individual functions or the whole translation unit, and then running long algorithms on these graphs).

When comparing lexical and syntactical analysis, it may indeed be the case that lexical analysis is more expensive. This is because both use state machines, i.e. there is a fixed number of actions per element, but the number of elements is much larger in lexical analysis (characters) than in syntactical analysis (tokens).

like image 91
Martin v. Löwis Avatar answered Jan 22 '26 07:01

Martin v. Löwis