Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The lexer groups individual characters into building blocks, tokens, that the language is made of. From keywords such as "if", "for", "function" and symbols like "{", to arbitrary identifiers that doesn't contribute to the actual meaning or structure of the program, i.e. "foo".

The parser groups the tokens into meaningful statements, it implements the grammar. If the parser sees an "if", it expects some condition next (depending on the grammar of course). If the parser detects statements that are not consistent with the grammar of the language, it should reject the program.

Tldr they work on different levels, lexers just finds the individual building blocks, the parser builds the program



The part I was missing was the idea of keeping ALL meaning/structure in the parser, and the lexer is used to identify chunks of characters that can be used to construct the meaning/structure of the program.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: