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.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Kategorie: Software & Web-Development

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

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

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

Helfen! Doppelte Bestelleinträge in meiner Verkaufstabelle

Oh, the bane of data! Right, duplicate entries – I feel your pain. Ever feel like your database is mocking you with mirrored realities? Why is d ...

Geschrieben von RodneyThompson am 11.10.2025 05:53:14
Forum: SQL / Datenbanken
json Rückgabe von Open Weather darstellen

Hallo Zusammen, ich habe vor über 20 Jahren das letzte mal aktiv php und HTM programmiert. Nun wollte ich für ein kleines Projekt zuhause mal w ...

Geschrieben von Chaos_Lord am 10.10.2025 21:50:09
Forum: PHP Developer Forum
mein PHP-Neustart und eine kleine Baustelle

Hallo Matthias, ich habe damals auf Empfehlung mit OOP und TE angefangen. OOP - objektorientierten Programmierung (verbesserte Code-Organisatio ...

Geschrieben von MichaelKre am 05.10.2025 18:02:34
Forum: User stellen sich vor
db Abfrage mit mehreren ids im WHERE

soso. Konnte ambiguous nicht so richtig verstehen. Jetzt bin ich schlauer. Danke.

Geschrieben von alfonso am 05.10.2025 16:31:52
Forum: PHP Developer Forum