nlsandler / write_a_c_compiler Goto Github PK
View Code? Open in Web Editor NEWTest suite to help you write your own C compiler
Home Page: https://norasandler.com/2017/11/29/Write-a-Compiler.html
License: MIT License
Test suite to help you write your own C compiler
Home Page: https://norasandler.com/2017/11/29/Write-a-Compiler.html
License: MIT License
https://norasandler.com/2017/12/28/Write-a-Compiler-4.html#fn1 links to http://web.cse.ohio-state.edu/~babic.1/COperatorPrecedenceTable.pdf which no longer exists.
Perhaps https://en.cppreference.com/w/c/language/operator_precedence would be a better link?
cloned your repo onto my mac and copied my compiler binary into the root. If i run the script from that directory i get this which is an error in the script:
STAGE 1
===================Valid Programs===================
./test_compiler.sh: line 28: ././stage_1/valid/multi_digit: No such file or directory
multi_digit..............FAIL
rm: ./stage_1/valid/multi_digit: No such file or directory
./test_compiler.sh: line 28: ././stage_1/valid/newlines: No such file or directory
newlines..............FAIL
rm: ./stage_1/valid/newlines: No such file or directory
./test_compiler.sh: line 28: ././stage_1/valid/no_newlines: No such file or directory
no_newlines..............FAIL
rm: ./stage_1/valid/no_newlines: No such file or directory
./test_compiler.sh: line 28: ././stage_1/valid/return_0: No such file or directory
return_0..............FAIL
rm: ./stage_1/valid/return_0: No such file or directory
./test_compiler.sh: line 28: ././stage_1/valid/return_2: No such file or directory
return_2..............FAIL
rm: ./stage_1/valid/return_2: No such file or directory
./test_compiler.sh: line 28: ././stage_1/valid/spaces: No such file or directory
spaces..............FAIL
rm: ./stage_1/valid/spaces: No such file or directory
===================Invalid Programs=================
missing_paren..............OK
missing_retval..............OK
no_brace..............OK
no_semicolon..............OK
no_space..............OK
wrong_case..............OK
===================Stage 1 Summary=================
6 successes, 6 failures
If i invoke my compiler manually i get this indicating it is working. Looks like a bug in the script?:
new-host-5:write_a_c_compiler james$ ./simple_c_compiler ./stage_1/valid/multi_digit.c
Found function name:main
Found number:100
CODEGEN: Expression
CODEGEN: Statement
CODEGEN: Function
CODEGEN: Program
EMMIT:.globl main
EMMIT:main:
EMMIT:mov $100, %eax
EMMIT:ret
Writing .globl main
Writing main:
Writing mov $100, %eax
Writing ret
<exp> ::= <id> "=" <exp> | <conditional-exp>
<conditional-exp> ::= <logical-or-exp> "?" <exp> ":" <conditional-exp>
This looks wrong - it forces every expression to be a conditional.
Something like the below would be better I think:
<conditional-exp> ::= <logical-or-exp> "?" <exp> ":" <conditional-exp> | <logical-or-exp>
stage_8/valid/nested_while.c
This has a % operator in, which is optionally implemented in the blog.
There's no token for = at stage 4, so invalid/split_le.c cannot pass the lexer (but the blog says all invalid files should).
split_le.c is:
int main() { return 1 < = 2; }
Hey! Following your guide at https://norasandler.com/2017/11/29/Write-a-Compiler.html, I can't help but get stuck on the first task (creating a simple lex() function); because it talks about creating a lexer using RegEx's, two things which I understand just fine, however it's not made clear the recommended way to go about this. I have done this in the past by looping through each character, appending it to a temporary string until a space, new-line or certain other characters are hit, and checking the string at each step, and I imagine there is a better way to do this using RegEx, but I'm just not seeing it. I have only ever used RegEx's to test if a given string matches the full expression.
A good format to use would be to give the task, then provide an expandable area containing the (or a, or multiple) solution(s) (preferably with code), and instructing the reader to try the task on their own first, and providing a little more insight into how the task should be accomplished.
...
Attached screenshots can be found below.
In week 2, the "-" operator is referred to as the "negation" operator, while in week 3 it's referred to as the "minus" operator. Is the difference intentional, and if so, why?
Thank you for creating this series of blogposts and the accompanying test suite. Looking forward to the release of the book!
Hi Nora,
just bought the early access "Writing a C Compiler" from No Starch Press. I wanted to ask about the code/scripts mentioned in the book, where can one find this code ?
Thanks,
Jean
Hey! Do you plan on continuing this series?
Files starting with skip_on_failure_ can't be lexed at stage 4.
The blog says "It should work for all stage 1-4 examples in the test suite, including the invalid ones."
fib.c has undefined behavior (UB) because it does not have a final return value
I have a question: I do not really understand how and why stage_3/valid/div_neg.c ((-12)/5;) should work. My code crashes with
30274 floating point exception (core dumped) ./div_neg
, whereas the result should be 254 (this is the result, when the programm is compiled with gdb).
Why should the output be 254?
s = $"{Generate(rootNode.Children[0])}" +
"push %rax\n" +
$"{Generate(rootNode.Children[1])}" +
"movl %eax, %ecx\n" + //move calculated divisor to %ecx
"pop %rax\n" + //pop divident do %eax
"cdq\n" +
"divl %ecx\n"; //eax contains the result, edx the rest
This is my code for the division. Any help would be appreciated!
This is my code: https://github.com/Clemens-Dautermann/lcc
In code generation phase of Logical AND, this code:
<CODE FOR e1 GOES HERE>
push %eax ;save value of e1 on the stack
<CODE FOR e2 GOES HERE>
pop %ecx ;pop e1 from the stack into ECX
; Step 1: SET CL = 1 iff e1 != 0
cmpl $0, %ecx ;compare e1 to 0
setne %cl ;set CL to 1 iff e1 != 0
; Step 2: SET AL = 1 iff e2 != 0
cmpl $0, %eax ;compare e2 to 0
movl $0, %eax ;zero EAX register before storing result
setne %al ;set AL to 1 iff e2 != 0
; Step 3: compute al & cl
andb %cl, %al ;store AL & CL in AL
should be:
<CODE FOR e1 GOES HERE>
push %eax ;save value of e1 on the stack
<CODE FOR e2 GOES HERE>
pop %ecx ;pop e1 from the stack into ECX
; Step 1: SET CL = 1 iff e1 != 0
cmpl $0, %ecx ;compare e1 to 0
movl $0, %ecx ; HERE:
setne %cl ;set CL to 1 iff e1 != 0
; Step 2: SET AL = 1 iff e2 != 0
cmpl $0, %eax ;compare e2 to 0
movl $0, %eax ;zero EAX register before storing result
setne %al ;set AL to 1 iff e2 != 0
; Step 3: compute al & cl
andb %cl, %al ;store AL & CL in AL
I think movl $0, %ecx
shoud be.
nitpick: This file is 'no_semicolon' but the first issue I hit was the missing parens for main.
Attached screenshots below
The trees in the attached screenshot are incredibly small when viewed using Mozilla Firefox, but appear to be normal when viewed using Google Chrome. Changing the height: auto;
declaration in the .post-content img
CSS selector to height: 15em;
seems to resolve the issue for me on Firefox, while still maintaining a normal view on Chrome (I checked).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.