-
Notifications
You must be signed in to change notification settings - Fork 171
[Parser] Implement parsing rules for functions #400
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
Conversation
Yup, Thanks @akshanshbhatt for showing your interest towards implementing |
Here at the last line, there is one grammatical conflict! |
After defining the grammar, For ex: function_def
: KW_DEF id "(" arg_list ")" ":" statements { } If you get the above error, then it is a success (now the grammar defining part is complete)! |
src/lpython/parser/parser.yy
Outdated
: KW_DEF func_name "(" arg_list ")" ":" { | ||
$$ = DEF_01(TODO); } | ||
| KW_DEF func_name "(" arg_list ")" ":" type_hint "->" type_hint { | ||
$$ = DEF_02(TODO); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will Define the grammar step by step. So at first, we will parse
def main():
print()
Here def
is KW_DEF
(which is a keyword)
main
is TK_NAME
ie., id
(identifier)
after :
there is a new line we can use sep
print()
appears in statement
There is an one INDENT
and DEDENT
By integrating we get:
function_def
: KW_DEF id "(" ")" ":" sep TK_INDENT statement TK_DEDENT { }
To get the tokens, you can do: $ lpython --show-tokens examples/expr2.py
(KEYWORD "def") 0:2
(TOKEN "identifier" main) 4:7
(TOKEN "(") 8:8
(TOKEN ")") 9:9
(TOKEN ":") 10:10
(NEWLINE) 11:11
(TOKEN "indent") 12:12
(TOKEN "identifier" print) 13:17
(TOKEN "(") 18:18
(TOKEN ")") 19:19
(NEWLINE) 20:20
(EOF) 21:21 |
Hi @akshanshbhatt, function defintion is a little difficult to parse, why don't you try while or for statements |
Here is the example program for while statement: i:i32 = 1
while i < 6:
print(i)
i += 1
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6") |
Yeah, sure! I'll first try to implement the grammar rules for looping statements. I'll ask you if I face any issues. |
@akshanshbhatt -- let us know if you need any help with this PR. |
15d6e44
to
ac9b23f
Compare
Sure, @Thirumalai-Shaktivel, I'll open a new PR. I've been a bit occupied lately with my end-semester examinations. I will complete this PR as soon as I get some time. |
Sure, Take your time (also, All the best for your remaining exams)! |
The new parser has not (yet) implemented the rules for function headers. I tried implementing it but got build errors -
or segmentation faults (in case it did compile) -
For now, I have commented out the template code. @Thirumalai-Shaktivel, can you please guide me?