Is lexer synonymous with parser?

Title - Question: Are the words "lexer" and "parser" synonymous, or are they different? Wikipedia seems to use the words interchangeably, but English is not my first language, so I cannot be sure.

+7
source share
5 answers

Not. The lexer splits the input into words; The parser discovers a syntactic structure between such "words". For example, this input:

velocity = path / time; 

Lexer output:

 velocity (identifier) = (assignment operator) path (identifier) / (binary operator) time (identifier) ; (statement separator) 

and then the parser can establish the following structure:

 = (assign) lvalue: velocity rvalue: result of / (division) dividend: contents of variable "path" divisor: contents of variable "time" 
+7
source

A lexer is used to split the input into tokens, while a parser is used to build an abstract syntax tree from this sequence of tokens.

Now you can simply say that tokens are just symbols and use the parser directly, but it is often convenient to have a parser that only needs to look at one token to determine what it will do next. Therefore, a lexer is usually used to divide input into tokens before the parser sees it.

A lexer is usually described using simple regex rules that are checked in order. There are tools like lex that can automatically generate lexers from such a description.

 [0-9]+ Number [AZ]+ Identifier + Plus 

The analyzer, on the other hand, is usually described by indicating grammar. Again, there are tools like yacc that can generate parsers from such a description.

 expr ::= expr Plus expr | Number | Identifier 
+8
source

Not. The lexer breaks the source text into tokens, while the parser correctly interprets the sequence of tokens.

+6
source

They are different.

The lexer takes a stream of input characters as input and outputs tokens (in other words, β€œtokens”).

The parser takes tokens (tokens) as input and produces (for example) an abstract syntax tree representing the operators.

Both are fairly similar, however, that quite a few people (especially those who have never written anything like this to a compiler or interpreter) view them the same way or (more often) use a "parser" when they really mean "lexer".

+3
source

As far as I know, lexer and parser make sense, but are not exact synonyms. Although many sources use them as similar ones, the lexer (an abbreviation of the lexical analyzer) identifies tokens related to the language from the input; while parsers determine if the token stream matches the grammar of the language in question.

+1
source

All Articles