Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

VB anyone use VB to develop a compiler?

views
     
TSSuperJunior
post Jul 18 2011, 05:44 PM, updated 15y ago

Getting Started
**
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..... icon_question.gif icon_question.gif icon_question.gif icon_question.gif icon_question.gif
alexa
post Jul 19 2011, 01:57 AM

Big Boss
******
Senior Member
1,456 posts

Joined: Jan 2009
From: mont kiara, kuala lumpur



i think u need customize your own DLL files
Draconian Paladin
post Jul 19 2011, 02:43 PM

Getting Started
**
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... smile.gif

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. smile.gif
TSSuperJunior
post Jul 19 2011, 04:05 PM

Getting Started
**
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... smile.gif

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. smile.gif
*
i understand what u said, but its quite complicated i think, haha
alexa
post Jul 19 2011, 04:23 PM

Big Boss
******
Senior Member
1,456 posts

Joined: Jan 2009
From: mont kiara, kuala lumpur



i think bill gates also don't how to build it
najminaruto
post Jul 20 2011, 12:42 PM

Out forever - this place is full of psychopaths
*****
Senior Member
746 posts

Joined: Jun 2006
From: Kuala Lumpur, Malaysia
QUOTE(alexa @ Jul 19 2011, 04:23 PM)
i think bill gates also don't how to build it
*
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.
Eventless
post Jul 20 2011, 01:04 PM

Look at all my stars!!
*******
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.....  icon_question.gif  icon_question.gif  icon_question.gif  icon_question.gif  icon_question.gif
*
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.
FlierMate1
post Jul 11 2022, 04:04 AM

Getting Started
**
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.
angch
post Jul 11 2022, 11:22 AM

On my way
****
Junior Member
636 posts

Joined: Jul 2006
Highly recommended: https://ruslanspivak.com/lsbasi-part1/
otai_g
post Jul 18 2022, 10:25 AM

On my way
****
Junior Member
623 posts

Joined: Sep 2008


not really understand what ur question. can we see the grammar file?
Lord Tiki Mick
post Jul 18 2022, 09:34 PM

Regular
******
Senior Member
1,020 posts

Joined: Jul 2012
QUOTE(otai_g @ Jul 18 2022, 10:25 AM)
not really understand what ur question. can we see the grammar file?
*
It's an 11 year old thread. sweat.gif

 

Change to:
| Lo-Fi Version
0.3740sec    0.66    5 queries    GZIP Disabled
Time is now: 25th December 2025 - 01:08 AM