d-u-d-e / c-compiler

GNU General Public License v3.0
3 stars 0 forks source link

Flexible parser with recursive descent parsing #20

Open d-u-d-e opened 1 week ago

d-u-d-e commented 1 week ago

Description 📌

With this PR we implement a basic version of the parser (pages 10-17 of the book).

The parser converts the list of tokens into an abstract syntax tree (AST), which represents the program in a form we can easily traverse and analyze.

Implementation Details 📝

Testing 🔎

Additional information ✨

Examples of running the driver as a standalone module:

~/c-compiler$ ./compiler_driver.py ../writing-a-c-compiler-tests/tests/chapter_1/valid/multi_digit.c --parse 
...
2024-11-14 21:00:23.326 | DEBUG    | compiler.parser.parsers:parse:127 - Parse tree: 
Program(
   FunctionDefinition(
      name=Identifier(main),
      body=Return(
         Constant(100)
      )
   )
)

In this case the source code contains:

int main(void) {
    // test case w/ multi-digit constant
    return 100;
}

If we run the driver with an invalid source file like:

int main( {
    return 0;
}

we get:

SyntaxError: Expected VoidKeyword but found OpenBrace
CremaLuca commented 6 days ago

I don't think having a folder for each type of test is useful, in most cases there will only be one file (test_xxx.py), so I believe it's better to keep them all under test/. For those special cases that require multiple classes, one can have multiple unittest.TestCase class inside one file.

albertoursino commented 3 days ago

We will write tests for the pretty printer with https://github.com/d-u-d-e/c-compiler/issues/26, and we will create another PR because we are introducing too many changes already with this one.