Parser examples

Beispiel #1 Simple calculator

<?php 

use Parle\{ParserParserExceptionLexerToken};

$p = new Parser;
$p->token("INTEGER");
$p->left("'+' '-' '*' '/'");

$p->push("start""exp");
$prod_add $p->push("exp""exp '+' exp");
$prod_sub $p->push("exp""exp '-' exp");
$prod_mul $p->push("exp""exp '*' exp");
$prod_div $p->push("exp""exp '/' exp");
$p->push("exp""INTEGER"); /* Production index unused. */

$p->build();

$lex = new Lexer;
$lex->push("[+]"$p->tokenId("'+'"));
$lex->push("[-]"$p->tokenId("'-'"));
$lex->push("[*]"$p->tokenId("'*'"));
$lex->push("[/]"$p->tokenId("'/'"));
$lex->push("\\d+"$p->tokenId("INTEGER"));
$lex->push("\\s+"Token::SKIP);

$lex->build();

$exp = array(
    
"1 + 1",
    
"33 / 10",
    
"100 * 45",
    
"17 - 45",
);

foreach (
$exp as $in) {
    if (!
$p->validate($in$lex)) {
        throw new 
ParserException("Failed to validate input");
    }

    
$p->consume($in$lex);

    while (
Parser::ACTION_ERROR != $p->action && Parser::ACTION_ACCEPT != $p->action) {
        switch (
$p->action) {
            case 
Parser::ACTION_ERROR:
                throw new 
ParserException("Parser error");
                break;
            case 
Parser::ACTION_SHIFT:
            case 
Parser::ACTION_GOTO:
            case 
Parser::ACTION_ACCEPT:
                break;
            case 
Parser::ACTION_REDUCE:
                switch (
$p->reduceId) {
                    case 
$prod_add:
                        
$l $p->sigil(0);
                        
$r $p->sigil(2);
                        echo 
"$l + $r = " . ($l $r) . "\n";
                        break;
                    case 
$prod_sub:
                        
$l $p->sigil(0);
                        
$r $p->sigil(2);
                        echo 
"$l - $r = " . ($l $r) . "\n";
                        break;
                    case 
$prod_mul:
                        
$l $p->sigil(0);
                        
$r $p->sigil(2);
                        echo 
"$l * $r = " . ($l $r) . "\n";
                        break;
                    case 
$prod_div:
                        
$l $p->sigil(0);
                        
$r $p->sigil(2);
                    echo 
"$l / $r = " . ($l $r) . "\n";
                        break;
            }
            break;
        }
        
$p->advance();
    }
}

Beispiel #2 Parse words out from a sentence

<?php

use Parle\{LexerTokenParserParserException};

$p = new Parser;
$p->token("WORD");
$p->push("START""SENTENCE");
$p->push("SENTENCE""WORDS");
$prod_word_0 $p->push("WORDS""WORDS WORD");
$prod_word_1 $p->push("WORDS""WORD");
$p->build();

$lex = new Lexer;
$lex->push("[^\s]{-}[\.,\:\;\?]+"$p->tokenId("WORD"));
$lex->push("[\s\.,\:\;\?]+"Token::SKIP);
$lex->build();

$in "Dis-moi où est ton papa?";
$p->consume($in$lex);
do {
    switch (
$p->action) {
    case 
Parser::ACTION_ERROR:
        throw new 
ParserException("Error");
        break;
    case 
Parser::ACTION_SHIFT:
    case 
Parser::ACTION_GOTO:
        
/* var_dump($p->trace());*/
        
break;
    case 
Parser::ACTION_REDUCE:
        
$rid $p->reduceId();
        if (
$rid == $prod_word_1) {
            
var_dump($p->sigil(0));
        } if (
$rid == $prod_word_0) {
            
var_dump($p->sigil(1));
        }
        break;
    }
    
$p->advance();
} while (
Parser::ACTION_ACCEPT != $p->action);

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Definition von Stored Procedures - eine Einführung

Stored Procedures sind vordefinierte SQL-Codeblöcke, die in einer Datenbank gespeichert sind und bei Bedarf aufgerufen werden können. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

What defines Laravel's developer community?

What factors contribute to the perceived happiness within the Laravel developer community, and how does this compare to other developer communitie ...

Geschrieben von BakhamMamodar am 01.05.2024 10:45:23
Forum: PHP Developer Forum
"How to warm cold entrance?"

What are some effective strategies for addressing the significant heat loss occurring in the entrance hall, which is noticeably the coldest area o ...

Geschrieben von NeerMohan am 01.05.2024 06:49:27
Forum: Fragen/Vorschläge zum Forum
Berechnungen durchführen

Hallo liebe Forenmitglieder, meine erste frage ist zum Aufbau meiner kleinen Berechnungswebseite, nichts kommerzielles, soll nur eine Anwendung f ...

Geschrieben von matze511 am 21.04.2024 21:42:37
Forum: PHP Developer Forum
Professioneller Webentwickler & Webdesigner

Of course, here is the translation: Hello, Thank you for your interest in the long-term project. Your extensive skills and experience in web dev ...

Geschrieben von Athelstan am 15.04.2024 09:25:39
Forum: Jobgesuche