Мне нужна помощь в написании грамматики BNF для этого perl кода - PullRequest
0 голосов
/ 07 февраля 2020

Я написал этот код после просмотра нескольких книг и видео. Я ищу помощь в написании грамматики для нее

    print "File name to read in: ";
    $fname = <STDIN>;
    chomp $fname;
    START:
    print "(N) for name (#) for number search and (.) to exit: ";
    $in = <STDIN>;
    chomp $in;
        if ($in eq "N") {
                %list = ();
                open ($input, "<",$fname) or die;                  # Open command will open the text file and < is used to read the contents                                                      
                while ($list =<$input>)                            # While loop is used to read all the elements until the end of the file.
                {                                                     
                    chomp ($list);                                    
                    my($Name, $number) = split(" ", $list);           # Spliting with the seperator as a single space
                    $list{$Name} = $number;                           # Defining value to the Key
                }
                print "Enter the Name to get the number:";
                $num = <STDIN>;
                chomp $num;
                print "$list{$num}\n";
                goto START;

        else {
                print "Invalid Input! \nPlease try again\n";
                goto START;
        }

Вот пример написанной мной грамматики.

<print> ::= <statement>
<statement> ::= <assign> ";"
<assign> ::= <lhs> "=" <rhs>
<lhs> ::= <var>
<rhs> ::= <expression>
<var> ::= ($|%|@)><name>
<name> ::=<alphabets|digits>|<name>
<alphabtes>::= <lowercase|uppercase>
<digits>::=<0|1|...|9>
<lowercase>::=<a|b|...|z>
<uppercase>::=<A|B|...|Z>
<assign>::= $fname = "<STDN> ";"
<STDN>::= input from keyboard.
<chomp>::= <var>
<var>::= $fname
...