tl;dr-ers:
How does a lexer normally deal with none-inline statements. statements that do not end with a specified statement delimiter. Such as control flow statements?
I believe that I have a fairly good grasp on lexical analysis, and can move on in my quest for understanding lexers/parsers. I do however, understand how lexers deal with 1multi-line statements.
After reading a Wikipedia article on the Comparison of programming languages syntax, The thing that all languages have in common, is that they have a very specfic statement delimeter. Some used semicolons(;
) as a statement delimiter, others use whitespace(\ws
), and some used periods(.
).
Using this method, i fail to see how those programming languages, are able to have functions\class\control flow, that span multiple lines. Correct me if I'm wrong, but I'm pretty sure that most of the popular programming languages(Python, Java, C, C++, C#, Javascript, etc...) Do not use their statement delimiters at the end of functions, or classes(I know that classes in C++ do use semicolons at there end, but that is besides the point), or control flow.
This means that either: A: Lexers make a special exception for statements that span multiple lines. Or B: The lexer just treats multi-line statements as a regular statement, and it is the parser's job to make sense of them.
For example, Take take this pseudo program in C++:
int exampleVar; //<-- inline statement. Delimited with a semicolon
void exampleFunc() { //<-- multi-line statement. This statement is the start of a block.
// do things
} //<-- this is where the statement that was started above, should end?
Clearly, it's easy to see where you should end the first statement. You end it at the semicolon. But how is the second statement dealt with? Is the statement extend to include everything up until the closing curly brace?
Or I could just be misguided in my thinking. It could be that the lexer has absolutely nothing to to do with 1multi-line statements. Is this the job of the parser? That is, is it the parser's job to make sense of 1multi-line statements?
To as clear as possible, my question is: How(if they should in the first place), should a lexer deal with statements that are not inline, and cannot just be terminated as if they were. How does a lexer normally deal with none-inline statements. statements that do not end with a specified statement delimiter. Such as control flow statements?
1To be clear, I do not mean multi-line statements, in the sense of line continuation. I mean them in the sense of statements that start off a block. Such as a function declaration. When you're defining a function, you also need to know the statements that follow the function definition up to a certain block delimiter. So you can't just terminate the statement after the definition.