Giter Site home page Giter Site logo

write_a_c_compiler's People

Contributors

chadbramwell avatar jmckitrick avatar nlsandler avatar saitouena avatar zuyoutoki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

write_a_c_compiler's Issues

Can't get the test script to work correctly

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:

new-host-5:write_a_c_compiler james$ ./test_compiler.sh ./simple_c_compiler

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

Stage 6 parser definition for conditional expressions looks incorrect.

<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 tests use modulo

stage_8/valid/nested_while.c

This has a % operator in, which is optionally implemented in the blog.

Stage 4, invalid/split_le.c cannot be lexed

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; }

Tasks should provide a solution

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.

AST changes on Week 5 are not all bolded

The AST addition "Assign(string, exp)" on week 5 is not bolded. First image shows the AST definition on week 4, and second image shows the unbolded addition of the Assign AST on week 5.
Screenshot 2024-01-24 005243
Screenshot 2024-01-24 005142

"-" operator changes names between Week 2 and Week 3

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!

Screenshot 2023-11-26 121220
Screenshot 2023-11-26 121316

Writing a C Compiler

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

Signed integer division

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

Logical AND mistake

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.

Trees on week 3 are really small when viewed using Firefox

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).

trees_for_ants
trees_for_humans

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.