# Creating an experimental syntax

- Thread: https://externals.io/message/45721
- Original archive: https://news-web.php.net/php.internals/45721
- Started by Rune Kaagaard on 2009-10-10T21:34:13+00:00
- Messages: 10
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #45721 — Rune Kaagaard — 2009-10-10T21:34:13+00:00

URL: https://externals.io/message/45721#45721

Dear Internals

At the moment I'm very inspired by functional programming languages
such as SML and especially scala. I would like to create and
alternative PHP syntax, just for fun nothing serious, inspired by
these languages. I've managed to compile PHP with debug stuff turned
on and found some files called zend_language_scanner.l,
zend_language_parser.y and did succed with creating an alias for
"function" called "def". I now wanted to change the function
definition into something more scala looking:

def max($x, $y) = {
  if ($x > $y) return $x else return $y
}

but got completely stuck.

I realised all of a sudden that I really don't understand anything
about what is going on in these files. Not even enough to ask good
questions. What should I do/read/study/buy/etc... ????

Thx in advance from a very confused dude that is in way over his head
Rune Kaagaard
Denmark

---

## #45723 — Alexey Zakhlestin — 2009-10-11T10:24:32+00:00

URL: https://externals.io/message/45721#45723 · In reply to: #45721

> Dear Internals
>
> At the moment I'm very inspired by functional programming languages
> such as SML and especially scala. I would like to create and
> alternative PHP syntax, just for fun nothing serious, inspired by
> these languages. I've managed to compile PHP with debug stuff turned
> on and found some files called zend_language_scanner.l,
> zend_language_parser.y and did succed with creating an alias for
> "function" called "def". I now wanted to change the function
> definition into something more scala looking:
>
> def max($x, $y) = {
>  if ($x > $y) return $x else return $y
> }
>
> but got completely stuck.
>
> I realised all of a sudden that I really don't understand anything
> about what is going on in these files. Not even enough to ask good
> questions. What should I do/read/study/buy/etc... ????
>
> Thx in advance from a very confused dude that is in way over his head

Well, in case of php-5.3+ there is partial support for functional  
paradigm.

try this:


<?php

$max = function($x, $y) {
     if ($x > $y)
         return $x;
     else
         return $y;
};

echo $max(5, 1);
echo $max(4, 99);
?>

http://docs.php.net/manual/en/functions.anonymous.php

---

## #45740 — Rune Kaagaard — 2009-10-12T11:51:10+00:00

URL: https://externals.io/message/45721#45740 · In reply to: #45721

Dear Internals

Ok... So i figured out that the way to make a scala style multi line
function declaration was simply to do this:

### zend_language_parser.y ###
unticked_function_declaration_statement:
               function is_reference `T_STRING` {
zend_do_begin_function_declaration(&$1, &$3, 0, $2.op_type, `NULL` 
TSRMLS_CC); }
                       '(' parameter_list ')' '=' '{' inner_statement_list '}' {
zend_do_end_function_declaration(&$1 TSRMLS_CC); }
;

### PHP ###
def new_function() = {
 echo "It works!\n";
}

But I have been stuck the last couple of hours trying to make support
for a one line function declaration without the '{}'. Example:

def double($x) = return 2 * $x;

I cant seem to find out what token to use for a new line? Any thoughts?

Thx a lot from confused guy
Rune Kaagaard
Denmark

---

## #45808 — Rune Kaagaard — 2009-10-14T11:15:51+00:00

URL: https://externals.io/message/45721#45808 · In reply to: #45721

Dear internals

I'm slowly getting to know more about bison and language parsing. A
new world for me. But progress is still slow. I have made an example
of the syntax I am looking to achieve at
http://code.google.com/p/php-alternative-syntax/ . If any of you c
gurus could help me with the best method to attack the problem I would
be very grateful!

Cheers
Rune Kaagaard
Denmark

---

## #45809 — Etienne Kneuss — 2009-10-14T11:37:36+00:00

URL: https://externals.io/message/45721#45809 · In reply to: #45808

Hello,

one problem that immediately pops out is that you have a parsing
ambiguity between an argument list and a concatenation of strings as a
single argument.

You should start by producing a BFN version of your desired grammar.
Such errors should then be more apparent.

Best,


> Dear internals
>
> I'm slowly getting to know more about bison and language parsing. A
> new world for me. But progress is still slow. I have made an example
> of the syntax I am looking to achieve at
> http://code.google.com/p/php-alternative-syntax/ . If any of you c
> gurus could help me with the best method to attack the problem I would
> be very grateful!
>
> Cheers
> Rune Kaagaard
> Denmark
>
> --
> 
> 
>
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #45810 — Etienne Kneuss — 2009-10-14T11:41:32+00:00

URL: https://externals.io/message/45721#45810 · In reply to: #45809

Hello,


> Hello,
>
> one problem that immediately pops out is that you have a parsing
> ambiguity between an argument list and a concatenation of strings as a
> single argument.
>
> You should start by producing a BFN version of your desired grammar.
> Such errors should then be more apparent.

*BNF


Also, the scala parser is quite complicated and some of its features
end up being quite tricky grammar-wise, porting scala-like grammars to
PHP would most likely require you to do major rewrites of the grammar,
I'd rather advise you to start with very small grammar changes.

Best,
>
> Best,
>
> 
> > […]
>
>
>
> --
> Etienne Kneuss
> http://www.colder.ch
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #45811 — Rune Kaagaard — 2009-10-14T11:57:51+00:00

URL: https://externals.io/message/45721#45811 · In reply to: #45810

Dear Etienne

Wauw did not know Backus–Naur Form before. That looks like a great
first step. Thx for showing me that!

Cheers
Rune Kaagaard
Denmark


> Hello,
>
> 
> > […]
>
> *BNF
>
>
> Also, the scala parser is quite complicated and some of its features
> end up being quite tricky grammar-wise, porting scala-like grammars to
> PHP would most likely require you to do major rewrites of the grammar,
> I'd rather advise you to start with very small grammar changes.
>
> Best,
> > […]
>
>
>
> --
> Etienne Kneuss
> http://www.colder.ch

---

## #45812 — Rune Kaagaard — 2009-10-14T11:58:44+00:00

URL: https://externals.io/message/45721#45812 · In reply to: #45811

Dear Etienne

Wauw did not know Backus–Naur Form before. That looks like a great
first step. Thx for showing me that!

Cheers
Rune Kaagaard
Denmark

---

## #45836 — Rune Kaagaard — 2009-10-17T14:22:39+00:00

URL: https://externals.io/message/45721#45836 · In reply to: #45809

Dear Internals

So I'm making good progress and found out that making simple syntax
changes is quite easy. What I currently got working can be seen here:
http://code.google.com/p/php-alternative-syntax/source/browse/trunk/scripts/kitchensink.php.

However, making functionality changes still baffles me. I changed the
"return" keyword to "<-", and I would like to automatically add a
return on the last line of a function. So that the following PHP code:

<?php
function foo() {
  $x = 2;
  $x * 2;
}
?>

would return an int with a value of 4.

How should I attack this problem?

Thx a lot!
Rune Kaagaard
Denmark

---

## #45837 — Etienne Kneuss — 2009-10-17T17:38:47+00:00

URL: https://externals.io/message/45721#45837 · In reply to: #45836

Hello,


> Dear Internals
>
> So I'm making good progress and found out that making simple syntax
> changes is quite easy. What I currently got working can be seen here:
> http://code.google.com/p/php-alternative-syntax/source/browse/trunk/scripts/kitchensink.php.
>
> However, making functionality changes still baffles me. I changed the
> "return" keyword to "<-", and I would like to automatically add a
> return on the last line of a function. So that the following PHP code:
>
> <?php
> function foo() {
>  $x = 2;
>  $x * 2;
> }
> ?>
>
> would return an int with a value of 4.
>
> How should I attack this problem?

This will be tricky to implement as you've to check if the last
statement of the function represents a value or not as Scala does, and
a lot of statements in PHP have no values: switch/while/for/if...

So function a() {
    if(true) .. else ..;
}

would have to implicitely return null, since if() is not a value.

>
> Thx a lot!
> Rune Kaagaard
> Denmark
>
> --
> 
> 
>
>



-- 
Etienne Kneuss
http://www.colder.ch
