Professional Documents
Culture Documents
Carpio Quiz Compiler Design
Carpio Quiz Compiler Design
Bermudo
BSCS-2A
1. 3 Parts of Compiler
# 1 PARSING
## a. Lexical Analysis/Tokenization (vocabulary)
## b. Syntactic Analysis/Parsing (grammar)
# 2 TRANSFORMATION
## a. Traversal
## b. Transform via Traversal
# 3 CODE GENERATION
## a. Stringify! # 1 PARSING
## a. Lexical Analysis/Tokenization (vocabulary)
## b. Syntactic Analysis/Parsing (grammar)
# 2 TRANSFORMATION
## a. Traversal
## b. Transform via Traversal
# 3 CODE GENERATION
## a. Stringify!
2. Tokenizer Function
3. Parsing Function
#The parse function creates an Abstract Syntax Tree given the computed
#tokens from the previous function
def parser(tokens):
#keep track of position while iterating
global current
current = 0
#nested walk function for building an abstract syntax tree
def walk():
#keep track of position while iterating?
global current
token = tokens[current]
#if a number is encountered, return a "NumberLiteral" node
if token.get('type') == 'number':
current = current + 1
return {
'type': 'NumberLiteral',
'value': token.get('value')
}
def main():
#test
input = "(add 2 (subtract 4 2))"
output = compiler(input)
print(output)
if __name__ == "__main__":
main()