Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tasks should provide a solution #21

Open
Logix-Dev opened this issue Jan 18, 2021 · 1 comment
Open

Tasks should provide a solution #21

Logix-Dev opened this issue Jan 18, 2021 · 1 comment

Comments

@Logix-Dev
Copy link

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.

@CDaut
Copy link

CDaut commented Jan 18, 2021

Mrs. Sandler provides this tutorial for free and puts her time and effort into providing this great educational ressource. So I think it's just a bit too much work to give this functionality. Aditionally you can always just take a look at her code. That should be the same as seeing the solution.
Anyway: I did it by just defining an Array of RegEx for the different Tokens and then loop over this array to check if any Token is matched at the beginning. If this is the case I remove the text from the beginning of the code and append a token to my token list. In some cases (identifier or intLiteral for example) you might have to do some additional stuff. Feel free to look at my code at https://github.com/Clemens-Dautermann/lcc/blob/master/Compiler/Lexer/Lexer.cs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants