gpt4 book ai didi

Simple recursive descent parser(简单递归下降解析器)

翻译 作者:bug小助手 更新时间:2023-10-26 22:45:28 33 4
gpt4 key购买 nike



I'm writing a simple recursive descent parser in Python that accepts the grammar ::= abc.
I'm using an array to store the input given by the user and validate that it's a correct one.

我正在用Python语言编写一个简单的递归下降解析器,它接受语法::=abc。我使用一个数组来存储用户提供的输入,并验证它是否正确。


The code that I have:

我拥有的代码是:


i = 0    # Global variables
t = ''
entry= ''

def nextToken():
global i
i += 1
return entry[i]

def A():
global t
if t == 'a': # Validation for the sequence abc that has to come in the input
t = nextToken()
if t == 'b':
t = nextToken()
if t == 'c':
t = nextToken()
else:
print("expected c")
else:
print("expected b")
else:
print("expected a")


print("Input to validate: ")
entry= input()
t = entry[i]
A()
if t == '':
print("Correct input ...")
else:
print("Input error...")

When running the code and typing abc which should be a valid entry for the code, it shows

当运行代码并输入abc(这应该是代码的有效条目)时,它显示



error: string index out of range



I'm thinking that the issue is on the entry array, but not sure how to solve it. I'm new in this and will appreciate all help.

我认为问题出在条目数组上,但不确定如何解决它。我在这方面是新手,我将感谢所有的帮助。


更多回答

Is entry supposed to be a list or string? You start with making it a list of 100 strings, but then entry = input() replaces it with a string.

条目应该是列表还是字符串?首先将其设置为包含100个字符串的列表,然后Entry=Input()将其替换为一个字符串。

it's supposed to be a string too, i change the variable to entry = ' '

它也应该是一个字符串,我将变量更改为Entry=‘’

Welcome to Stack Overflow. Please read How to Ask. We do not find the bug for you here; we require a specific question - which will come out of your best attempt to understand and locate a specific problem, and showcase it in a minimal reproducible example.

欢迎来到Stack Overflow。请阅读如何提问。我们在这里没有为您找到错误;我们需要一个特定的问题-这将来自您尽最大努力了解和定位特定问题,并在最小的可重现的示例中展示它。

The way you're handling nextToken is named and written like C. Python has a builtin docs.python.org/3/library/functions.html#next Using that would have solved the error when you get to the end. Also, you don't need any globals -- it would be easier to just pass "entry" to the function.

您处理nextToken的方式的命名和编写类似于C。Python有一个内置的docs.python.org/3/库/unctions.html#Next,使用它可以在您完成操作时解决错误。此外,您不需要任何全局变量--只将“Entry”传递给函数会更容易。

优秀答案推荐

After you've checked each of the three letters in the entry, you call nextToken() an extra time. This tries to index off the end of the string, which is what's causing your exception.

检查完条目中的三个字母后,再调用nextToken()一个额外的时间。这会尝试索引字符串的末尾,这就是导致异常的原因。


It looks to me like your code expects C-style null-terminated strings. In C, a pointer just off the end of a string (at the null byte) is equivalent to an empty string.

在我看来,您的代码需要C风格的以空结尾的字符串。在C中,一个刚离开字符串末尾(空字节)的指针相当于一个空字符串。


Here's what your various string variables would look like in C (at the end of the code, where you test t == ''):

下面是各种字符串变量在C中的样子(在代码的末尾,测试t==‘’):


| a | b | c | \0 |      | \0 |
^ ^ ^
| | |
entry t ''

But that's now how strings are represented in Python. Indexing off the end of the string causes an exception.

但这就是Python中字符串的表示方式。索引超出字符串的结尾将导致异常。


There are a few different ways you could resolve the issue. One idea would be to use try and except to catch the exception that gets raised in nextToken() and do something appropriate (e.g. setting t to an empty string).

有几种不同的方法可以解决这个问题。一种想法是使用Try和Except来捕获在nextToken()中引发的异常,并执行一些适当的操作(例如,将t设置为空字符串)。


But another idea would be to use slicing rather than simple indexing in nextToken(). A slice off the end of a string will be empty, rather than raising an exception. Try this:

但是另一个想法是在nextToken()中使用切片而不是简单的索引。字符串末尾的片段将为空,而不是引发异常。试试这个:


def nextToken():
global i
i += 1
return entry[i:i+1] # change is here

更多回答

Yes, I was given a similar example in c++ with a different grammar, the structure was similar and needed to adapt it so i can use my own grammars. The code now works correctly and I can use that validation for some more instructions. Thank you so much for the help.

是的,我得到了一个用C++编写的类似的例子,但语法不同,结构相似,需要调整它,这样我才能使用我自己的语法。代码现在可以正常工作了,我可以使用该验证来获取更多指令。非常感谢你的帮助。

Make sure you do check the end, lest you incorrectly accept "abcd"

一定要检查结尾,以免错误地接受“abcd”

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com