Jump to content

Recommended Posts

Posted (edited)

Hi all!

I am working on a AutoIt grammar in the Peggy syntax, and have recently started working towards being able to output the generated parser in AutoIt syntax via a Peggy plugin.

The parser/grammar is not yet 100% AutoIt syntax compatible, but I think the generated parser is now in an acceptable state.

If you have any performance optimization suggestions, let me know! 😄

When the plugin is ready for release, I will add a GitHub link to latest GitHub release generated asset, for the parser, but for now i will just attach the script here, and the grammar with modified code blocks.

The parser outputs an abstract syntax tree, representing the parsed input.

Example:

#include <Array.au3>
#include "parser.au3"

$ast = peg_parse("2 * (3 + 4)")

If Not (@error = 0) Then
    ConsoleWrite("Error: "&@error&@CRLF)
    Exit 1
EndIf

$result = Process($ast)
_ArrayDisplay($result)

Func Process($ast)
    Switch $ast.type
        Case 'Program'
            Local $body = $ast.body
            Local $results[UBound($body)]
            For $i = 0 To UBound($body) - 1
                Local $node = $body[$i]
                $results[$i] = Process($node)
                If Not (@error = 0) Then Return SetError(@error)
            Next
            Return $results
        Case 'ExpressionStatement'
            Local $result = Process($ast.expression)
            If Not (@error = 0) Then Return SetError(@error)
            Return $result
        Case 'BinaryExpression'
            Local $left = Process($ast.left)
            If Not (@error = 0) Then Return SetError(@error)
            Local $right = Process($ast.right)
            If Not (@error = 0) Then Return SetError(@error)

            Switch $ast.operator
                Case '+'
                    Return $left + $right
                Case '-'
                    Return $left - $right
                Case '*'
                    Return $left * $right
                Case '/'
                    Return $left / $right
                Case '&'
                    Return $left & $right
                Case Else
                    ConsoleWriteError("Unknown operator: "&$ast.operator&@CRLF)
                    Return SetError(1)
            EndSwitch
        Case 'Literal'
            Return $ast.value
        Case 'ParenthesizedExpression'
            Local $result = Process($ast.expression)
            If Not (@error = 0) Then Return SetError(@error)
            Return $result
        Case Else
            ConsoleWrite("Unknown node type: "&$ast.type&@CRLF)
            Return SetError(1)
    EndSwitch
EndFunc

Link to grammar: autoit3.pegjs

Link to Peggy plugin: peggy-au3.ts

parser.au3 autoit3-modified.pegjs

Edited by genius257
Fixed spelling
Posted (edited)

Thanks @genius257, but I cannot run the example code above 😔 .

I created a folder, pasted your two attached files in and created a example.au3 file with the code above.
When I try to run this code (in VSCode) or try to use CTRL+F5 to call the Au3Check, I get several warnings and errors.

⚠ I also have to say: At the moment I use the VSCode extension of Loganch, yours is disabled for now. Is that a problem 🤔 ?  Are there other possible dependencies like your "vscode-autoit3-debug"?

Here my VScode OUTPUT when I try to run the example above:

Spoiler
"C:\Program Files (x86)\AutoIt3\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "c:\Store\Repositories\playground\autoit-parser-by-genius257\example.au3" [PID 31520]
+>21:29:13 Starting AutoIt3Wrapper (21.316.1639.1) from:Code.exe (1.97.0.0)  Keyboard:00000407  OS:WIN_10/2009  CPU:X64 OS:X64  Environment(Language:0407)
>Running AU3Check (3.3.16.1)  from:C:\Program Files (x86)\AutoIt3  input:c:\Store\Repositories\playground\autoit-parser-by-genius257\example.au3
"c:\Store\Repositories\playground\autoit-parser-by-genius257\parser.au3"(3879,66) : warning: $TYPES_TO_PROPERTY_NAMES: possibly used before declaration.
        ($tail[$i])[$TYPES_TO_PROPERTY_NAMES[($tail[$i])["type"]]]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"c:\Store\Repositories\playground\autoit-parser-by-genius257\parser.au3"(3879,77) : error: Statement cannot be just an expression.
        ($tail[$i])[$TYPES_TO_PROPERTY_NAMES[($tail[$i])["type"]]] = $result
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"c:\Store\Repositories\playground\autoit-parser-by-genius257\parser.au3"(6109,37) : warning: $a: possibly used before declaration.
     $m["body"] = $body = Null ? $a :
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"c:\Store\Repositories\playground\autoit-parser-by-genius257\parser.au3"(6109,37) : error: $a: undeclared global variable.
     $m["body"] = $body = Null ? $a :
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
c:\Store\Repositories\playground\autoit-parser-by-genius257\example.au3 - 2 error(s), 2 warning(s)
!>21:29:14 AU3Check ended.rc:2
+>21:29:14 AutoIt3Wrapper Finished.
!>Exit code 2 Time: 0.947

 

I guess at least the two errors should not exist, right?

Best regards
Sven

Edited by SOLVE-SMART

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...