VB anyone use VB to develop a compiler?
VB anyone use VB to develop a compiler?
|
|
Jul 18 2011, 05:44 PM, updated 15y ago
Show posts by this member only | Post
#1
|
![]() ![]()
Junior Member
207 posts Joined: Oct 2008 |
my lecturer gave me a grammar file, ask us to develop a compiler to check is the source program are match with the grammar file or not~ anyone here develop a compiler using VB before, help me pls~~ i can send u the grammar file once u request for it, thx..... and pls.... help me.....
|
|
|
|
|
|
Jul 19 2011, 01:57 AM
Show posts by this member only | Post
#2
|
![]() ![]() ![]() ![]() ![]() ![]()
Senior Member
1,456 posts Joined: Jan 2009 From: mont kiara, kuala lumpur |
i think u need customize your own DLL files
|
|
|
Jul 19 2011, 02:43 PM
Show posts by this member only | Post
#3
|
![]() ![]()
Junior Member
80 posts Joined: Jan 2006 |
Basically, you need to read source code, then make sense of the source code according to the grammar rule...then generate code. That's very rough description of how compiler work, to my own understanding...
The idea is you will have 3 modules or components, or whatever you want to call it. The first one will simply read your source file, and pass the "token" or words in the source file to the next module. Second module is where the grammar rule will be applied. Basically, it is something along the line of "if the next token is 'if', then do a, else if the next token is 'while', then do b" Then, when the compiler "do a" it will read the condition, read the block of code that is inside the if block and execute code as it goes. Of course this is determined by your grammar. And second module is closely related to your grammar. This is usually known as Parser and the first module, which is known as Lexer is sometimes considered to be part of the Parser as well, since Lexer can be as simple as one function. Now, when your compiler "do a", it will probably call the appropriate codegen function in the third module to generate the assembly lang/bytecode/machine code to produce an object file. Since you are using VB, you can use .Net Reflection library to generate object code... That's it, your compiler can target .Net framework and use Reflection library to output .Net bytecode instead of assembly lang. Hope this help. |
|
|
Jul 19 2011, 04:05 PM
Show posts by this member only | Post
#4
|
![]() ![]()
Junior Member
207 posts Joined: Oct 2008 |
QUOTE(Draconian Paladin @ Jul 19 2011, 02:43 PM) Basically, you need to read source code, then make sense of the source code according to the grammar rule...then generate code. That's very rough description of how compiler work, to my own understanding... i understand what u said, but its quite complicated i think, hahaThe idea is you will have 3 modules or components, or whatever you want to call it. The first one will simply read your source file, and pass the "token" or words in the source file to the next module. Second module is where the grammar rule will be applied. Basically, it is something along the line of "if the next token is 'if', then do a, else if the next token is 'while', then do b" Then, when the compiler "do a" it will read the condition, read the block of code that is inside the if block and execute code as it goes. Of course this is determined by your grammar. And second module is closely related to your grammar. This is usually known as Parser and the first module, which is known as Lexer is sometimes considered to be part of the Parser as well, since Lexer can be as simple as one function. Now, when your compiler "do a", it will probably call the appropriate codegen function in the third module to generate the assembly lang/bytecode/machine code to produce an object file. Since you are using VB, you can use .Net Reflection library to generate object code... That's it, your compiler can target .Net framework and use Reflection library to output .Net bytecode instead of assembly lang. Hope this help. |
|
|
Jul 19 2011, 04:23 PM
Show posts by this member only | Post
#5
|
![]() ![]() ![]() ![]() ![]() ![]()
Senior Member
1,456 posts Joined: Jan 2009 From: mont kiara, kuala lumpur |
i think bill gates also don't how to build it
|
|
|
Jul 20 2011, 12:42 PM
Show posts by this member only | Post
#6
|
![]() ![]() ![]() ![]() ![]()
Senior Member
746 posts Joined: Jun 2006 From: Kuala Lumpur, Malaysia |
QUOTE(alexa @ Jul 19 2011, 04:23 PM) he actually build the BASIC interpreter for Altair and very much involved in the development of Visual Basic himself.BTW TS, have you build the language grammar yet? then you can build the parser using VB and translate it to VB code. |
|
|
|
|
|
Jul 20 2011, 01:04 PM
Show posts by this member only | Post
#7
|
![]() ![]() ![]() ![]() ![]() ![]() ![]()
Senior Member
2,643 posts Joined: Jan 2003 |
QUOTE(SuperJunior @ Jul 18 2011, 05:44 PM) my lecturer gave me a grammar file, ask us to develop a compiler to check is the source program are match with the grammar file or not~ anyone here develop a compiler using VB before, help me pls~~ i can send u the grammar file once u request for it, thx..... and pls.... help me..... Based on this, I don't think the lecturer is asking TS to build an actual compiler. This is more like a parser to check the syntax of the code rather than a compiler for creating executable programs. |
|
|
Jul 11 2022, 04:04 AM
Show posts by this member only | IPv6 | Post
#8
|
![]() ![]()
Validating
139 posts Joined: Jun 2022 |
I only learn about lexer and parser 11 years later (from the date of this thread). For example, given the "source code" below Code n := 5; p := 1; while n > 0 do p := p * n; n := n - 1 end It will get analyzed as: Code ('n', 'ID') (':=', 'RESERVED') ('5', 'INT') (';', 'RESERVED') ('p', 'ID') (':=', 'RESERVED') ('1', 'INT') (';', 'RESERVED') ('while', 'RESERVED') ('n', 'ID') ('>', 'RESERVED') ('0', 'INT') ('do', 'RESERVED') ('p', 'ID') (':=', 'RESERVED') ('p', 'ID') ('*', 'RESERVED') ('n', 'ID') (';', 'RESERVED') ('n', 'ID') (':=', 'RESERVED') ('n', 'ID') ('-', 'RESERVED') ('1', 'INT') ('end', 'RESERVED') Result(CompoundStatement(CompoundStatement(AssignStatement(n, IntAexp(5)), AssignStatement(p, IntAexp(1))), WhileStatement(RelopBexp(>, VarAexp(n), IntAexp(0)), CompoundStatement(AssignStatement(p, BinopAexp(*, VarAexp(p), VarAexp(n))), AssignStatement(n, BinopAexp(-, VarAexp(n), IntAexp(1)))))), 25) The first half is lexer, the second half is parser. I think it builds AST (abstract syntax tree). As posters have mentioned earlier, I think the lecturer was not asking to build a fully-fledged compiler, maybe just a front-end compiler. Lord Tiki Mick liked this post
|
|
|
Jul 11 2022, 11:22 AM
Show posts by this member only | Post
#9
|
![]() ![]() ![]() ![]()
Junior Member
636 posts Joined: Jul 2006 |
Highly recommended: https://ruslanspivak.com/lsbasi-part1/
|
|
|
Jul 18 2022, 10:25 AM
|
![]() ![]() ![]() ![]()
Junior Member
623 posts Joined: Sep 2008 |
not really understand what ur question. can we see the grammar file?
|
|
|
Jul 18 2022, 09:34 PM
|
![]() ![]() ![]() ![]() ![]() ![]()
Senior Member
1,020 posts Joined: Jul 2012 |
QUOTE(otai_g @ Jul 18 2022, 10:25 AM) It's an 11 year old thread. otai_g liked this post
|
| Change to: | 0.3740sec
0.66
5 queries
GZIP Disabled
Time is now: 25th December 2025 - 01:08 AM |